我是Docker的新手,由于缺少ssl证书,我在Docker中运行应用程序时遇到了问题。如何使用开发证书在非开发环境的docker中运行aspnetcore应用程序?
我有以下由Visual Studio生成的Dockerfile
:
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文件(appsettings.docker.json
(,因为在Docker中运行时需要一些不同的值,并且我已经在使用我的appsettings.development.json
从VS进行标准运行。因此我设置了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'.
基于这个答案,我发现当环境变量为Development
时,dotnet会自动生成dev-cert。如何在一个不同的环境中解决这个问题?
我确实尝试过在Dockerfile中使用dotnet dev-certs https
命令,但是构建失败了,说映像中没有sdk,因此不知道该命令。
尝试添加这两行
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"]