skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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, change FROM scratch to FROM 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 in csproj with the ContainerImageName element :

    ...
      <PropertyGroup>
        ...
        <TargetFramework>net7.0</TargetFramework>
        <ContainerImageName>dotnet-worker-image</ContainerImageName>
      </PropertyGroup>
    
      <ItemGroup>
        ...
        <PackageReference Include="Microsoft.NET.Build.Containers" Version="7.0.401" />
      </ItemGroup>
    </Project>
    

    After that, you can publish directly to an image with :

    dotnet publish --os linux --arch x64 /t:PublishContainer -c Release
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search