Search code examples
c#.netdockerdockerfiledocker-image

How to create a docker image of self-contained dotnet 7 console application


I have published my console app using this command:

dotnet publish -c Release --self-contained.

I have created a dockerfile inside the project folder and this is the content of that:

FROM scratch
COPY bin/Release/net7.0/win-x64/publish .
ENTRYPOINT ["JustForDocker.dll"]
  • My console app just prints hello world.
  • I have tried to build this docker image using the command:
docker build -t dockername .
  • I created the docker image and tried to run it using the command: docker run dockername
  • But I cannot see the output I can just see a blank output, nothing else, but it should print hello world as output.

Solution

  • A self-contained .NET app isn't going to work in a scratch container without more native dependencies. It's only self-contained in terms of .NET runtime. But the .NET runtime still depends on native libraries such as OpenSSL. Instead, you'll want to make use of the official runtime-deps container images produced for .NET: https://github.com/dotnet/dotnet-docker/blob/main/README.runtime-deps.md.

    So if you change FROM scratch to FROM mcr.microsoft.com/dotnet/runtime-deps:7.0, that should give you what you're after.