App opens console window when being build with Docker

Viewed 414

I'm facing the issue that a .Net Core WPF application automatically opens a console window when started. This only happens when build inside a Docker container. When I build it directly on my PC, only the actual application window opens.

My best guess is that this is an issue with the operating system the .Net Core image is based on. The .Net Core SDK Docker Hub Repo knows the following tags: 3.1-nanoserver-1809, 3.1-nanoserver-1903, 3.1-nanoserver-1909, 3.1-nanoserver-2004, 3.1-nanoserver-2009. I was able to confirm the issue with the first three tags, but the 2004 and 2009 tags do not run on my machine, so I need someone to try this out and either confirm my theory (which would mean that it should not happen on at least on of these images) or to come up with a better explanation of why this is happening.


This is reproducible with the default .Net Core WPF app Visual Studio creates for you. Here is a Dockerfile to test it out:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src

COPY . ./
RUN dotnet build -c Debug -o out

FROM stefanscherer/chocolatey
WORKDIR /app

RUN choco install -y 7zip

# Depending on your project setup it might be src/[project name]/out
COPY --from=build /src/out ./test 

RUN 7z a -y test.zip ./test/*

You can build the image and extract the compiled program with the following commands:

docker build -t testimage .
docker run -d --name testcontainer testimage
docker stop testcontainer
docker cp testcontainer:app/test.zip .

2 Answers

I was able to reproduce this problem on both 3.1-nanoserver-2009 and 3.1-nanoserver-2004 for you.

I think the problem is related to the warning printed out during build:

warning NETSDK1074: The application host executable will not be customized because adding resources requires that the build be performed on Windows (excluding Nano Server).

If that's the case, then it seems that it is a limitation of the nanoserver base image, and unfortunately it looks like this problem still has not been resolved, because it's still present when building in mcr.microsoft.com/dotnet/nightly/sdk:5.0.

Here's a related pull request that might shed some light on the subject.

Having said that, I think the only option for now is to use windows image other than nanoserver (alternatives can be found here). I didn't find any image that would come with .NET Core SDK preinstalled (I didn't put much effort into finding it though), but it should be fairly simple to set it up. In the following example I used servercore image since it is much more lightweight than windows image.

FROM mcr.microsoft.com/windows/servercore:20H2 AS sdk
WORKDIR /dotnet
# Download the official .NET Core install script
RUN powershell -c "Invoke-WebRequest -Uri https://dot.net/v1/dotnet-install.ps1 -OutFile dotnet-install.ps1"
# Run the install script
RUN powershell -c "& ./dotnet-install.ps1 -InstallDir ."
# Add the installed executable to PATH
RUN setx PATH "%PATH%;/dotnet"

FROM sdk AS build
# Do your stuff here

Here you'll find the documentation for the install script.

I also did confirm that the produced application did not spawn console window when run.

As @Grx70 mentioned, the underlying nanoserver image is the problem. There is another .NET core image, but it is based on the servercore image. This saves the manual installation of .NET Core within the image and has everything ready at hand.

Here you can find all version of the .NET Core Image: DockerHub

Way down at the bottom, you'll find the heading Windows Server Core 2019 amd64 Tags and the associated mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2019 image.

This image no longer produced a console window when I built something :)

Related