Sunday 4 December 2022

Run .NET Core 3.1 Application with Windows Server Core 2019 Docker Image

 You would love to run your .NET applications as Linux containers instead of using Windows containers.  However, there are situations that you have to run your .NET applications as Windows containers with servercore docker image due to your project having dependencies, such as a third party dll (could be even built by different department in your company) which demands your application to run on Windows servercore docker image. Worst case is sometimes your app is still on .NET 3.1 Core you do not have a servercore image for .NET Core 3.1. (For .NET 5,6,7,8 the Windows servercore images are available.. check here)  Therefore you need to setup .NET Core 3.1 runtime on the servercore docker image. Let's see how we can achive this requirement.

Using below base image defintion in your docker file you could setup .NET Core 3.1

The follwing will allow you to use windows server core 2019 image.

# Windows server core image (4.42GB) used due to dependency
FROM AS base

Then you can setup .NET Core 3.1 runtime with below in your docker file.

# Install .NET Core 3.1 runtime
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
ADD C:/setup/dotnet-install.ps1
RUN C:/setup/dotnet-install.ps1 -Runtime aspnetcore -Channel 3.1 -Version latest -InstallDir '/Program Files/dotnet'
ENV DOTNET_ROOT="c:\Program Files\dotnet"
# Add .NET Core to path
RUN setx /M PATH $(${Env:PATH} + ';C:\Program Files\dotnet')

Once you setup your base image it can be used to run your app like below (how to build your app with dockerfile is skipped here for brevity).

FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "mydemoapp.dll"]

No comments:

Popular Posts