Build and run an ASP.NET Core application with Docker

The topic of this post is a new technology that widespread in the world of the virtualization, that is Docker.

It is based on the concept of the containers, every application that runs on docker is a container, that is a lightweight executable package that contains all the dependencies that the application needed to run.

For instance, an ASP.NET Core application container will contains, among other things, the Core Framework.

This approach give us the advantage of deploy the application without take care about the configurations of the hosting environment; furthermore, the container is lighter and easier to manage than a traditional virtual machine and is preferred when we want to manage applications separately.

If we want to configure an ASP.NET Core application with Docker, we have some steps that we need to do.

Docker installation

The first thing is install the docker environment to run the containers.

We can refer to this page to choice the right installation; in my case I have a Windows Home desktop, so i need to install the Docker Toolbox instead of Docker for Windows.

The installation is very simple and, based on your windows version, if you don’t have Hiper-V installed (used from docker to create local machines), will be installed VirtualBox as well.

Once the installation is completed, we can run the docker environment,  the terminal will be showed:


Docker configuration file

Once the environment is ready we have to configure our application and we have to add the Dockerfile in the root of the application.

It’s like a configuration file where we says to Docker the instructions to build the container:

FROM microsoft/aspnetcore-build AS builder
WORKDIR /source

COPY *.csproj .
RUN dotnet restore

COPY . .
RUN dotnet publish --output /app/ --configuration Release

FROM microsoft/aspnetcore
COPY --from=builder /app .
ENTRYPOINT ["dotnet", "WebApp.dll"]

The file is composed by two parts, the first one deal with the build of the project; with the instruction FROM I say what is the base image for my container, that is the Microsoft image with the ASP.NET Core build environment; this container has all the stuff that we needed to build a project, like the .NET Core Framework.

After we have defined the WORKDIR I copy the csproj in the root of the container path (the second parameter “.” means root path) and then I run the dotnet restore command.

Next I copy the rest of the project and then I run dotnet publish command with the app folder as a parameter.

Now it’s the time of the second part of the configuration file, that is the configuration of the startup of the application.

In this case, the startup image is aspnetcore, that has the ASP.NET Core runtime already installed; I define the app folder as workdir and I copy the built package to this folder.

The last row is the entry point of the container, that is the startup command to be executed; in my case I startup my web application.

Build and run

Now I’m ready to prepare and run my app in a Docker container.

The first thing that I need to do is build the application with a docker command.

So I go to the root on the web application and I run this command:

docker build -t webapp .

This command build the docker image from which the containers will be generated.

The start point for every container is an image that the docker daemon will use to build a container when requested.

If the command end successfully we are ready to run a new container from that image:

docker run --detach -p 5000:80 --name webapp_container webapp

The command is quite simple, with the option detach we says to docker that we don’t need an interactive console of the container, so we haven’t to manage it; with the p parameter we define the port exposed to the outside and the port which the other containers can use to comunicate; the name parameter is the name associated to the container.

After we have executed the command, we can check the running containers with this command:

docker ps -a

The result of this command should looks like this.


In the first row I have the container up and run, but before to access the application from a web browser, we need to retrieve the ip of the docker virtual machine.

We can do this with the command:

docker-machine ip

Now I can type in my browser:


And the web application responds!

That’s all, we I have installed docker on my machine, configured the dockerfile for my application, built the image and run the docker container.


Build and run an ASP.NET Core application with Docker

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s