Exploring .net core docker file


One of the major features of Microsoft .NET core is its ability to run as a cross platform framework on multiple OS(Linux, Mac, Windows) and on containerized environments. An effort by Microsoft to continue to support containerized environments and enhance CI/CD pipelines. And to help get started, Microsoft provided a number of different docker images available for use based on common use cases. In this post I will talk about working with containers and Dockerfiles in .NET Core.

Essentially, A Dockerfile is a plain text containing set of instructions, each instruction lists the steps required to create a working image.The .NET Dockerfile is no exemption. For example: set an environment variable, copy a file, or run a script. Whenever an instruction is run, a new layer is created on top of the initial Docker base images. Docker base image is the starting point image on which layers are built on (usually consisting of a file System structure with OS configuration) to create all of your container images. For example, in order to run a LAMP stack as a Docker containers, you might use either an Ubuntu 14.04 or CentOS 7, or any of your Linux OS as a base image. Then, you would install Apache, MySQL and PHP on it and the result would be your final LAMP Docker image, which can be run as a container.

Naïve (BASIC) dockerfile sample.

FROM: creates a layer from the ubuntu:15.04 Docker image.
COPY: add’s (copy) files from your project current directory to Docker the host path.
RUN: builds your application with make.
CMD: specifies what command to run within the container.

Dockerfile Commands

FROM: The FROM instruction indicates the base image and initialize a new build stage for subsequent instructions and operation. For .NET core apps, it is important that the right root image be selected

use for deploying self-contained deployment apps (compiles into DLLs)

use for deploying .NET Core console apps

use for deploying ASP.NET Core apps (compiles into DLLs and runs app)

– use for building .NET Core (or ASP.NET Core apps) Sets the default URL for apps to http://+:80

COPY: The COPY instruction copies new files or directories from (project root folder) and adds them to the filesystem of the container at the path (Docker host path). Conventionally, .NET docker files are located in the project root folder a sample copy instruction would be

RUN: The RUN instruction will execute any command, on top of the current image and continue to the next instruction. The resulting committed image will be used for the next step in the Dockerfile. .NET core commands like dotnet restore, dotnet build commands are would be executed using the RUN command.

CMD: The CMD commands is to provide defaults for an executing container. There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.
ENTRYPOINT: An Entrypoint allows you to configure a container that will run as an executable.
WORKDIR: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
If a relative path is provided, it will be relative to the path of the previous WORKDIR instruction. For example:

The output path Dockerfile would be /app/src/assets.

.NETcore docker file

The listen above is a functional .net docker file for a project named. The note above should help explain the files.
From the root of our sample solution using PowerShell
docker build -t dockerApp .
Docker will now begin to build my image. Downloading and installing several components and packages.


After running a successful, build

This tells Docker to run the image called dockerApp.


microsoft/dotnet:-runtime-deps – use for deploying self-contained deployment apps
microsoft/dotnet:-runtime – use for deploying .NET Core console apps
microsoft/dotnet:-aspnetcore-runtime – use for deploying ASP.NET Core apps
microsoft/dotnet:-sdk – use for building .NET Core (or ASP.NET Core apps)

The .NET Core SDK – This is what you need to build .NET Core applications.
The .NET Core Runtime – This is what you need to run .NET Core applications.


Docker for dotnet now enables .net apps to run completely on containerized environments without any code changes.coupled with the advantages that comes with containerization. This post explains some of the common Docker file commands the images used in .NET Core development. Each of the images have a set of specific use-cases, and it’s important you use the right one for your requirements.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

John Odey is a FrontEnd software developer who have strong passion for bringing conceptual ideas into real software solutions.


Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *