How to Create a .NET Docker Container in Six Easy Steps

Over the last few years, containers and Docker have gone from buzzwords to industry standards in software development. Even though Docker and containers are now mainstays in software development, many developers are still confused and even intimidated by the tech. This tutorial aims to simplify these concepts and demonstrate how easy it can be to create a .NET Docker image and then run a container from that image and even get the image ready for a service like Amazon EKS or Azure ACS.

The Solution

In this tutorial we will create a simple .NET console application along with a simple Dockerfile in order to create a very simple .NET Docker Container.

Remember, for any example solution from AWS with .NET, we focus on the code that exemplifies the problem we are trying to solve. We don’t include logging, input validation, exception handling, etc., and we embed the configuration data within classes instead of using environment variables, configuration files, key/value stores and the like. These items should not be skipped for proper solutions.


To complete this solution you will need the .NET CLI which is included in the .NET SDK. .NET 5 is being used in this tutorial. In addition, you will need to install Docker.

Warning: some AWS services may have fees associated with them.

Let’s start by creating a simple .NET console application by running the following command at the CLI.

$ dotnet new console ––name dotnetapp

This command creates a canned .NET console application with the name of dotnetapp. If we change the directory/folder to “dotnetapp” and run the app with the dotnet run command, we can see that the app displays a message of “Hello World!”

$ dotnet run

Now that we have a running .NET application, let’s focus on publishing the application. To publish the .NET application, run the dotnet publish command like so:

$ dotnet publish ––output buildoutput

This command simply builds and publishes the application to the buildoutput folder.

With the application published, we’ll use six simple steps to build our docker image.

First, create a file in the “dotnetapp” directory/folder named, Dockerfile.

Our second step involves referencing a base image in the Dockerfile by using the FROM instruction. For our image, we are going to start with a Microsoft base image that includes the .NET 5 runtime. So, line one of the Dockerfile is as follows:


Third, we need to set our working directory. The WORKDIR instruction sets the working directory for any subsequent instructions. If the provided working directory doesn’t exist, Docker will create the folder/directory for us. We’ll set the working directory with the following Dockerfile instruction.

WORKDIR /buildoutput

Notice, we set the working directory to the folder/directory that we published the application to.

Step four, now that we have our working directory set, we are going to copy all of the files from the application folder to the image by using the following Dockerfile instruction.

COPY ./buildoutput .

We’re almost done — in step 5 we now need to tell Docker how to bootstrap the application by referencing the dotnet command and passing it the name of the published DLL. The last line of the Dockerfile will be the following Dockerfile instruction:

ENTRYPOINT [“dotnet”, “dotnetapp.dll”]

This is the sixth and last step — from within our application folder, “dotnetapp”, let’s build our Docker container by running the Docker build command at the CLI.

$ docker build -t dotnetapp:1.0 .

This command builds our Docker image using our published folder/directory, our Dockerfile and the .NET 5 base image. The command also tags the image as dotnetapp version, 1.0.

That’s it. You now have a very simple .NET 5 Docker image. This image can be used with services like Amazon EKS and Azure ACS, among others. To create and run a container locally using this image, use the following command:

$ docker run dotnetapp:1.0 -it

Upon completion, you should see the “Hello World!” text in the console.

Get all the source code at GitHub!