Getting Started With ASP.NET Core And Docker

Hi everyone!

Often, while working with Docker, beginners face great problems understanding how to use a Dockerfile, its common commands, and why we have to use it. Moreover, another problem comes to .NET developers who want to package their ASP.NET Core applications in one single image in a container and they don’t know which image they need to use to run the apps. In this post, we will see how to make it in a very clear manner.

How to package an ASP.NET Core app in the container with Docker?

To package an ASP.NET Core app in a container, there are three steps.

  1. Create your ASP.NET Core project
  2. Write a Dockerfile that will describe how to construct your image
  3. Create a container that will make your image alive or allow the execution of your image like a single process.

Create your ASP.NET Core Project

The first thing to do is to create a project. For this, we have to make sure that .NET Core is installed on our machine. However, in this example, we will create a simple ASP.NET Core WebAPI. It is the same procedure for other sets of projects. For this, run the following commands in the Command Prompt,

mkdir dockerapp
cd dockerapp
dotnet new webapi

After that, you will have a functional API. To test it, run this command.

dotnet build
dotnet run

Type this address -

localhost: 5000/api/values
to get the values from ValuesController.

So, we have finished step one. Finally, here we have our WebAPI that returns [“value1”, ”value2”] in JSON.

Write a Dockerfile

Before we go through the problem, we have to know what a Dockerfile is. Briefly, Dockerfile is a file that contains a series of instructions which define how to construct an image.

Common command

FROMThis command specifies the base image, eg: Microsoft/aspnetcore:2.0
WORKDIRChanges the working directory for subsequent instructions in the Dockerfile
COPYCopies files from source to destination
RUNExecutes a command directly in the container
EXPOSESets a specific port internally to the container
VOLUMEUsually, it is used to map a physical directory on our machine to the logical directory in the container instead.
ENTRYPOINTSpecifies which application will run in the container created from the image.

OK. Now, we will use some of these instructions to write our Dockerfile. First, in your WebAPI Project, create a file named Dockerfile without any extension.


When we write Dockerfile for ASP.NET Core app, we must first know if we want to use the Development Environment or the Deployment Environment.

Development Environment

In it, we need an image that contains a compiler and allows compiling directly in the container, and for that, we use Microsoft/aspnetcore-build directly from the DockerHub (Docker Repository).

Deployment Environment

Here, we do not need a compiler because we will have to build our application externally from our image, integrate all built files in the image, and just use a lightweight image containing the .NET Core Runtime Microsoft/aspnetcore to execute our application.

In this case, we will work in the Development Environment that is little bit complex than Deployment Environment.

Copy and paste all these instructions in your Dockerfile,

  1. #Development Environment Process   
  2. FROM microsoft/aspnetcore-build:latest AS build-env  
  3. WORKDIR /app  
  5. #copy csproj and restore distinct layers,make sure that we all dependencies  
  6. COPY *.csproj ./  
  7. RUN dotnet restore  
  9. #Copy everything else and build the project in the container  
  10. COPY . ./  
  11. RUN dotnet publish --configuration Release --output dist  
  13. #Deployment Environment Process   
  14. #Build runtime image  
  15. FROM microsoft/aspnetcore:latest  
  16. WORKDIR /app  
  17. COPY --from=build-env /app/dist ./  
  18. EXPOSE 80/tcp  
  19. ENTRYPOINT [ "dotnet","dockerapp.dll" ]  

 After that, in cmd type

docker build. –t rootandadmin/dockerapp –f Dockerfile

Now, we have our image and to check the type.

docker images

Create a Container

Now, we have our image rootandadmin/dockerapp but as this image is dead we have to make it alive. For this we need to create a container. There is a big difference between Image and Container; a container is a process that contains an image and executes it.

Then to create a container from our image type this.

docker create command –p 555:80 –name rootandadmin/dockerapp01 rootandadmin/dockerapp

docker start rootandadmin/dockerapp01

All is ready. You can try to access your API in the browser using the following address.

localhost: 555/api/values