ASP .NET Core with Docker: Beginers Guide

Image for post
Image for post

Hi Developer

If you are looking for information about ASP.NET and Docker you came to the right site.

This article will teach you how to create and build an ASP .NET Core application and how to use Docker in your app. Before starting to code let’s talk about these technologies for enter in context.

What is ASP .NET Core?

A technical explaination could be a little bit confusing and boring, so I will explain it with our development language to get it quickly
The simple answer is:

ASP.NET is a platform provided by microsoft in the early 2000s that allows developers to create web apps, services, and dynamic websites.

You can use Visual Basic or C# languages to code an ASP.NET application, but the most common practice is code in C#.

What about .NET Core? NET Core is the framework that support ASP.NET, is a cross platform, open source re-implementation of the .NET Framework. If you want to know more in depth, I recommend you this wonderful post about .NET Core.

If you are familiarized with C#, this will be very easy to you to start with ASP.NET, believe me.

Image for post
Image for post
C# code in a ASP.NET Core Application

ASP.NET makes easy to create MVC Applications. It provides a languange named razor to build views. Basically, allow to insert C# code inside HTML, reducing coding and increasing the productivity.

Image for post
Image for post
Razor code

In the code above we use a “foreach” inside of HTML code to print a list of contacts.

There are plenty of good reasons to use ASP.NET when we are developing a website or an application; High speed, low cost, and vast language support are among the most significant benefits. ASP.NET also allows a developer to choose from many different development environments, also its popularity gives a developer tons of online resources to learn new things, or troubleshooting bugs. Also, if you used C# in the past, you could have good reasons for use ASP.NET.

Docker, containers, VMs and more…

Before explain what docker is, I am going to talk about a problem we can find out, then we can understand how to solve this problem and why use dockers is important to us as developers.

Imagine you need to develop an application and you don’t know who will use it or where it will be hosted.

Image for post
Image for post

There are different kind of applications that can be run in different operating systems. Maybe you could compile the code for most populars operating system and solve the issues. But think… when you want to make changes you will have to recompile all again for different platforms and the maintenance is a big problem.

Image for post
Image for post

You could put yours applications and its dependencies inside a box. The operating system should give you support to the box , and it doesn’t matter what it contains.

Image for post
Image for post

Now, you just need to develop for one platform, you will assure that the program runs inside the box, the same box for each operating system. That is exactly what docker does.

So, what is docker?

Docker is a technology that allows the creation and use of containers, basically it is a tool that allows developers and sys-admins to easily deploy their applications in a sandbox (containers) to run on the host operating system i.e. Linux.

You can think “Are Virtual Machines and Docker containers the same?, they both do exactly the same”

The anserw is no.. Virtual Machines and Docker Containers are useful tools to solve the same problem, but they uses different techniques. To understand this you should first know what is virtualization.

“Virtualization is like a school bus. Instead of each parent driving their kid to school each day in their car with the resulting traffic jams and waste of time and fuel or having to build lots of extra lanes on the roads”

So in computer world, Virtualization can be defined as one physical computer performing the same function as multiple computers or A single highly-specified computer is loaded with special software which allows resources to be managed & shared, that special software is called hypervisor.
in this way, a single powerful machine would contain multiple virtual machines

Image for post
Image for post

So now you are using resources efficiently, for example your pc now have multiple Operating Systems installed, this is because to hypervisor.

Docker does this task more efficiently, in whole new fantastic way.

Image for post
Image for post

Main differences you would note here is that Docker is more efficient usage of the underlying system and resources. VMs are great at providing full process isolation for applications, but this isolation comes at great cost.

Containers take a different approach: by leveraging the low-level mechanics of the host operating system, containers provide most of the isolation of virtual machines at a fraction of the computing power.

Building the application

Install .Net Core SDK.
Install Docker.
A text editor (like VSCode).

So, let’s go 👊

We are going to create a MVC application using the powerful tools of .NET Core.
If you are not familiarized with de MVC pattern, here you can get an easy explanation. Then come back when you finish it.

You can clone the project from github

git clone

Or you can code with me…

Open your terminal and let’s work from it

mkdir AspNetCore-Docker
cd AspNetCore-Docker
mkdir src
cd src
mkdir AspMVC
cd AspMVC

Once you are inside the AspMVC directory, follow the next steps:

  • Create a new MVC project from a .NET Core templatedotnet new MVC
  • Open the AspMVC folder with your code editor

You will see how net core create all the files needed in a ASP.NET App

Image for post
Image for post
ASP.NET MVC Application files

Open Views → Home → Index.cshtml

Image for post
Image for post
A Razor page in ASP.NET

The above image shows you how a razor page is, It looks like a HTML page because it uses HTML, but also we can put C# code inside. Let’s try it.

Image for post
Image for post
A Razor page in ASP.NET

Note that I’ve added two lines. The first line is a common h1 HTML tag. Second line is a h2 tag, but it calls the UtcNow property from DateTime class. The “@” symbol indicates to compiler that we are using C# code.

Now, in the terminal, write th next command dotnet restore
this restores the dependencies and tools of a project.

Next step is to publish our app: dotnet publish -o ./publish
This command packs the application and its dependencies into a new folder named publish. Now, it is ready to run anywhere (or almost anywhere).
Go to the publish folder cd publish

You should see something like this:

Directorio: C:\Users\wilfr\Documents\Programacion\dotnet\AspNetCore-Docker\src\AspMVC\publishMode                LastWriteTime         Length Name
---- ------------- ------ ----
d----- 17/6/2018 19:14 wwwroot
-a---- 17/6/2018 00:33 178 appsettings.Development.json
-a---- 17/6/2018 00:33 113 appsettings.json
-a---- 17/6/2018 19:14 279974 AspMVC.deps.json
-a---- 17/6/2018 01:16 8192 AspMVC.dll
-a---- 17/6/2018 01:16 1728 AspMVC.pdb
-a---- 17/6/2018 19:14 75264 AspMVC.PrecompiledViews.dll
-a---- 17/6/2018 19:14 46592 AspMVC.PrecompiledViews.pdb
-a---- 17/6/2018 19:14 221 AspMVC.runtimeconfig.json
-a---- 17/6/2018 00:33 628 bundleconfig.json
-a---- 17/6/2018 19:14 380 web.config

Then, run the application dotnet AspMVC.dll

Our application is now running and listening on the port 5000, so open the browser and go to http://localhost:5000

What you can see in the picture below is the MVC template provided by the framework and the two lines that we’ve added.

Image for post
Image for post
ASP.NET MVC Template

As you can see, is really easy to build and ASP.NET application…

Dockerizing the app

Once we have created the application, we can make a snapshot of it and put it inside a container. We need to create a Dockerfile with the instructions needed to run our application anywhere. Let me give you a brief definition:

A dockerfile is how Docker images and the deployment of an application are described. It’s a basic file and you may only require a few lines to get started with your own image. In other words, it is a file that contains a step by step instructions to deploy the image inside the container.

Image for post
Image for post

You can get into a depth instructions at oficial documentation of docker.

Let’s continue creating the Dockerfle

Make a new file in your root directory with the name “Dockerfile”

cd ../../..# Make sure you are in the root directory! ---> AspNetCore-Docker
# Create new file with the name Dockerfile (extension is not required)

The first line we are going to add is FROM microsoft/aspnetcore:2.0
It will to download the aspnetcore image from the hub repository, so it actually contains the .NET Core and you don’t need to put it inside the image. You can find more repositories and versions in the docker hub.

Second line WORKDIR /app sets our working directory in the app folder, inside the container we are building.

Then, we need to copy the contents of the publish folder into the app folder on the image COPY ./src/AspMVC/publish .

The last line that we are going type in the Dockerfile is the ENTRYPOINT statement ENTRYPOINT ["dotnet", "AspMVC.dll"] Basically, this line tells to docker it should run the dotnet command with AspMVC.dll as parameter.

Your Dockerfile should looks like this

Our docker file is ready, the only thing left is to do is to build our image, let’s do it

In your terminal,go to the root folder (where the Dockerfile is) and run the following commands:

  1. docker build -t myimage .
    It tells docker “build the image, with the friendly name “myimage” , in the root directory”

You will see in the log how docker runs step by step the Dockerfile

Sending build context to Docker daemon 8.338 MB
Step 1/4 : FROM microsoft/aspnetcore:2.0
2.0: Pulling from microsoft/aspnetcore
cc1a78bfd46b: Pull complete
bc828d97c1a6: Pull complete
026b941ef1b4: Pull complete
b7ed7a19c735: Pull complete
c9da504e42d6: Pull complete
Digest: sha256:5f964756fae50873c496915ad952b0f15df8ef985e4ac031d00b7ac0786162d0
Status: Downloaded newer image for microsoft/aspnetcore:2.0
---> cdc2d48122e4
Step 2/4 : WORKDIR /app
---> d29e8ea14550
Removing intermediate container 57797c092dc6
Step 3/4 : COPY ./src/AspMVC/publish .
---> 70a21b4998e0
Removing intermediate container 28870cd9153c
Step 4/4 : ENTRYPOINT dotnet AspMVC.dll
---> Running in 6383bc22233e
---> 5eccd8fdb3c7
Removing intermediate container 6383bc22233e
Successfully built 5eccd8fdb3c7

2. docker run -p 5000:80 myimage
Tells docker “run the image myimage and publish the container’s port to host on port 5000.

warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {4b54feda-9616-4a83-be21-f2007a5997c4} may be persisted to storage in unencrypted form.
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

Testing your image

Go to localhost (or your url host), you will be the application running inside the container.

Note tha my url is not localhost because my files are hosted in

Image for post
Image for post
Application running inside the container

If you’ve followed these steps, you should not have any issues to view your application in the browser and you are ready!

Thanks for reading, if any help required please comment below or send me a mail to

Get the Medium app