visual-studiodocker.net-coredockerfileserver-core

Issue running Docker container from VS2019


I have built a .Net Core 3.0 console app that generates pdf reports using Telerik. Telerik leverages the GDI+ library to do this. The app consumes an internally developed dll that has a dependency on the Windows events logs so unfortunately hosting on Linux is currently out of the question.

I am attempting to run this app in a docker container but struggling to get it to work via Visual Studio 2019 when using the full Windows server core image. As far as I understand this is the only image that has GDI+ libraries I need.

When using servercore:1803 I receive this error when running via Visual Studio: "Unable to start program 'C:\Program Files\dotnet\dotnet.exe'. The system cannot find the path specified."

Error message received from Visual Studio 2019 Based on the logs output from Container Tools and Build it seems everything worked as expected.

Here is my complete dockerfile. It is exactly what Visual Studio adds when you select "Add --> Docker Support..." with the exception of the images used. Note when I use 3.0-nanoserver-1803 Visual Studio runs the container as expected but fails when the report generation code requiring GDI is executed.

FROM mcr.microsoft.com/windows/servercore:1803 AS base
#FROM mcr.microsoft.com/dotnet/core/runtime:3.0-nanoserver-1803 AS base
WORKDIR /app   

FROM mcr.microsoft.com/windows/servercore:1803 AS build
#FROM mcr.microsoft.com/dotnet/core/sdk:3.0-nanoserver-1803 AS build
WORKDIR /src
COPY ["DM.Web.Reporting.Background/DM.Web.Reporting.Background.csproj", "DM.Web.Reporting.Background/"]
RUN dotnet restore "DM.Web.Reporting.Background/DM.Web.Reporting.Background.csproj"
COPY . .
WORKDIR "/src/DM.Web.Reporting.Background"
RUN dotnet build "DM.Web.Reporting.Background.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "DM.Web.Reporting.Background.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DM.Web.Reporting.Background.dll"]

Am I doing something wrong here? I'm a Docker noob so hopefully I just missed something simple.


Solution

  • This would be because you've made the change from a .NET Core runtime and SDK base images to the Windows Server Core image:

    FROM mcr.microsoft.com/windows/servercore:1803 AS build
    #FROM mcr.microsoft.com/dotnet/core/sdk:3.0-nanoserver-1803 AS build
    

    The Server Core image does not have .NET Core installed in it. You'll need to install it as part of your Dockerfile.

    I've modified your Dockerfile to install the .NET Core runtime in the runtime stage and the SDK in the SDK stage:

    FROM mcr.microsoft.com/windows/servercore:1803 AS base
    #FROM mcr.microsoft.com/dotnet/core/runtime:3.0-nanoserver-1803 AS base
    
    # Install .NET Core Runtime
    RUN powershell -Command \
            $ProgressPreference = 'SilentlyContinue'; \
            Invoke-WebRequest \
                -UseBasicParsing \
                -Uri https://dot.net/v1/dotnet-install.ps1 \
                -OutFile dotnet-install.ps1; \
            ./dotnet-install.ps1 \
                -InstallDir '/Program Files/dotnet' \
                -Channel 3.0 \
                -Runtime dotnet; \
            Remove-Item -Force dotnet-install.ps1 \
        && setx /M PATH "%PATH%;C:\Program Files\dotnet"
    
    WORKDIR /app   
    
    FROM mcr.microsoft.com/windows/servercore:1803 AS build
    #FROM mcr.microsoft.com/dotnet/core/sdk:3.0-nanoserver-1803 AS build
    
    # Install .NET Core SDK
    RUN powershell -Command \
            $ProgressPreference = 'SilentlyContinue'; \
            Invoke-WebRequest \
                -UseBasicParsing \
                -Uri https://dot.net/v1/dotnet-install.ps1 \
                -OutFile dotnet-install.ps1; \
            ./dotnet-install.ps1 \
                -InstallDir '/Program Files/dotnet' \
                -Channel 3.0; \
            Remove-Item -Force dotnet-install.ps1 \
        && setx /M PATH "%PATH%;C:\Program Files\dotnet"
    
    WORKDIR /src
    COPY ["DM.Web.Reporting.Background/DM.Web.Reporting.Background.csproj", "DM.Web.Reporting.Background/"]
    RUN dotnet restore "DM.Web.Reporting.Background/DM.Web.Reporting.Background.csproj"
    COPY . .
    WORKDIR "/src/DM.Web.Reporting.Background"
    RUN dotnet build "DM.Web.Reporting.Background.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "DM.Web.Reporting.Background.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "DM.Web.Reporting.Background.dll"]
    

    See https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script for more details on how to use the dotnet-install script.

    UPDATE:

    For more general purpose instructions on how .NET Core can be installed in a Dockerfile (Linux or Windows), see https://github.com/dotnet/dotnet-docker/blob/master/samples/snippets/installing-dotnet.md.