0

我是 Docker 新手,由于缺少 ssl 证书,我在 docker 中运行我的应用程序时遇到了麻烦。如何使用开发证书在具有非开发环境的 docker 中运行 aspnetcore 应用程序?

我有以下Dockerfile由 Visual Studio 生成:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
COPY [...other libraries...]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]

当我使用默认设置(开发)从 Visual Studio 运行它时,它连接 Docker 并且一切运行良好。appsettings.docker.json但是,我想使用不同的 appsettings 文件appsettings.development.json(因此我设置了ASPNETCORE_ENVIRONMENT=Docker. 这引起了我的痛苦,因为我突然感到InvalidOperationException

System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.

To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.

基于这个答案,我发现 dotnet 在环境变量为Development. 对于不同的环境,如何解决这个问题?

我确实尝试dotnet dev-certs https在 Dockerfile 中使用该命令,但是,构建失败说映像中没有 sdk,因此不知道该命令。

4

1 回答 1

0

尝试添加这 2 行

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
# Add the line below ---------------------------------------------------
RUN dotnet dev-certs https
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
COPY [...other libraries...]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
# Add the line below ---------------------------------------------------
COPY --from=publish /root/.dotnet/corefx/cryptography/x509stores/my/* /root/.dotnet/corefx/cryptography/x509stores/my/
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]
于 2022-02-18T06:16:10.870 回答