Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dev certificate on aspnetcore docker container

I'm new to Docker and I have run into a trouble running my app in docker due missing ssl certificates. How do I run aspnetcore application in docker with non-development environment using dev certificates?

I have the following Dockerfile generated by 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"]

When I run it from the Visual Studio with the default setup (development), it connects Docker and everything runs fine. However, I want to use a different appsettings file (appsettings.docker.json) because I need some different values when running in Docker and I'm already using my appsettings.development.json for my standard run from the VS. Thus I set ASPNETCORE_ENVIRONMENT=Docker. This is causing a pain as I'm suddenly getting an 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'.

Based on this answer, I have figured out that dotnet generates the dev certs automagically when the environment variable is Development. How can that be fixed for a differently called environment?

I did try to use the dotnet dev-certs https command in the Dockerfile, however, build failed saying that there is no sdk in the image and thus doesn't know the command.

like image 268
Vočko Avatar asked Sep 14 '25 06:09

Vočko


1 Answers

Try adding these 2 lines

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"]
like image 125
Hans Kilian Avatar answered Sep 16 '25 18:09

Hans Kilian