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 the see the output i can just see a blank output, nothing else, but it should print hello world as output.
2
Answers
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 officialruntime-deps
container images produced for .NET: https://github.com/dotnet/dotnet-docker/blob/main/README.runtime-deps.md.So if you change
FROM scratch
toFROM mcr.microsoft.com/dotnet/runtime-deps:7.0
, that should give you what you’re after.Self-contained doesn’t mean native. The runtime is included but the runtime’s dependencies are not. You need to either use the mcr.microsoft.com/dotnet/runtime-deps image as a base, or use .NET 7’s Publish as container functionality. Another option is to use AOT to produce a native binary.
To use the
runtime-deps
image, changeFROM scratch
toFROM mcr.microsoft.com/dotnet/runtime-deps:7.0
To publish to a container image directly, you need to add the
Microsoft.NET.Build.Containers
package to the project and specify the container image name incsproj
with theContainerImageName
element :After that, you can publish directly to an image with :