Search code examples
.netdockerubuntusslcontainers

In wsl Ubuntu I can't copy the SSL certificate into the docker container


I am trying to run my .Net 7 REST application, which is connected to a postgresql database, in Ubuntu 22.04 via docker container. The application works in Windows 10 and now I am trying to get it to work in Linux as well. For this I used

dotnet dev-certs https -ep ${HOME}/.aspnet/https/aspnetapp.pfx -p 1234
dotnet dev-certs https --trust

and released all rights with

sudo chmod 777 aspnetapp.pfx

In my Docker-compose.yaml I try to copy the certificate into the container via volume. My file looks like this:

version: '3.8'

networks:
  mynetwork:
     driver: bridge

services:

   notification:
      build: .
      ports:
      - "8080:80"
      - "8081:443"
      environment:
         ASPNETCORE_URLS: "https://+;http://+"
         ASPNETCORE_HTTPS_PORT: "8001"
         ASPNETCORE_Kestrel__Certificates__Default__Password: "1234"
         ASPNETCORE_Kestrel__Certificates__Default__Path: "${HOME}/.aspnet/https/aspnetapp.pfx"
      volumes:
        - ${HOME}/.aspnet/https/:/https/
      depends_on:
         - postgres
      networks:
         - mynetwork

   postgres:
       image: postgres:latest
       container_name: postgres
       hostname: postgres
       ports:
       - "9876:5432"
       environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=1234
          - POSTGRES_DB=mail
       networks:
          - mynetwork
       volumes:
          - ./database_data:/var/lib/postgresql/data

The Postgresql container works and is executed, but the notification application gives the error " Could not find a part of the path '/home/dennis/.aspnet/https/aspnetapp.pfx'."

If I enter the command

cd ${HOME}/.aspnet/https/

in the terminal, I get to the directory and the file is there. But Docker can't find the directory and I don't understand why. I hope someone can help me. For completeness, here is the Docker file.

#build stage
FROM mcr.microsoft.com/dotnet/sdk:7.0 as build
WORKDIR /src
COPY . .
RUN dotnet restore "./Notification/Notification.csproj" --disable-parallel
RUN dotnet publish "./Notification/Notification.csproj" -c release -o /app --no-restore

#Serve stage
From mcr.microsoft.com/dotnet/sdk:7.0
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT="Docker"
COPY --from=build /app .

Solution

  • Change this line:

    ASPNETCORE_Kestrel__Certificates__Default__Path: "${HOME}/.aspnet/https/aspnetapp.pfx"

    to this:

    ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx

    The environment attribute in your compose file sets environment variables in a service's containers. ${HOME}/.aspnet/https/ seems to be a directory on your host machine, not your container.

    References:

    1. https://docs.docker.com/compose/environment-variables/set-environment-variables/#use-the-environment-attribute
    2. Asp.Net Core docker-compose https self signed certificate issue