Showcasing ASP.NET Core with Docker

So, what exactly is .NET?

.NET Core logo

This is an extract from the Microsoft Docs:

.NET is a general purpose development platform. It has several key features, such as support for multiple programming languages, asynchronous and concurrent programming models, and native interoperability, which enable a wide range of scenarios across multiple platforms.

The .NET framework can be used to develop all sorts of applications. But it has a downside, they will only work on Windows environments. This is where .NET Core comes in:

.NET Core is an open-source, general-purpose development platform maintained by Microsoft and the .NET community on GitHub. It’s cross-platform (supporting Windows, macOS, and Linux) and can be used to build device, cloud, and IoT applications.

So basically, .NET Core is the open source and cross-platform version of the .NET Framework, neat!

Since we will building a simple web application, we will focus on ASP.NET, which is the web part of .NET:

ASP.NET is a free web framework for building great websites and web applications using HTML, CSS, and JavaScript. You can also create Web APIs and use real-time technologies like Web Sockets.

What is Docker?

Docker logo

This is the Docker overview, from the official docs:

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Docker will help us in creating an environment in which our app will run, so that we can test it and prepare to deploy it.

The main benefits of using Docker instead of a VM is that you don’t need to install a whole OS just to run your app, Docker creates these tiny containers that share your OS kernel and are really lightweight. You can have them up and running in almost no time, which can be a HUGE advantage to us developers.

Dependencies problems are a thing of the past with Docker, each container will work no matter what, since it already has everything that is necessary to run your specific app.

Getting started

We will use Visual Studio Code as our text editor, and the .NET Core SDK to develop a simple web application which will then be deployed to a Docker container.

This article is aimed to people that have previous experience developing applications and want to check out the .NET environment.

You can find this whole project in this Github repo!


I’ve used a computer with Windows 10 as my OS, but every piece of software used here is cross-platform so you should not have any trouble adapting what I did here to your preferred OS.

Visual Studio Code Install

Installing VS Code is really easy, just go the website and download the installer for your platform:

Once the download is complete, run the installer and follow the on screen instructions. There are no special options that require extensive knowledge.

.NET Core Install

You will need to download the latest version from Microsoft’s website. Just click “Download .NET Core SDK” and then execute the file, the installation has no options and will complete fairly quickly.

Docker Install

Installing Docker is pretty straightforward, just go to the Docker website and click on “Get Started”.
You’ll then need to select “Download Docker for Windows” and you will be redirected to the download page.
If you do not have an account, create one and then proceed to download the installer.

Once you’ve downloaded the executable file, open it. You will be greeted with the following screen:

Click “Ok” and the program will install itself. You will then need to log out of your Windows session:

Once you log back in, Docker will ask your permission to enable certain required features:

After you click “Ok” your computer will reboot and then Docker will be ready to use! Enter your credentials into the popup on the right side of the screen:

Creating the project

Make a new folder in a safe directory, this is where our project files will be located. Once inside VS Code we can open that folder and then bring up the terminal by going into View — Terminal.

There we can type: dotnet new mvc -o DockerTest

This will create a new MVC type project into a directory named “DockerTest”. Now, type cd DockerTest, to move the terminal into the directory that contains the actual project files and then execute dotnet run.

Terminal output

The project will now be executed and you can see on the terminal output that it is now running on two different ports: 5000 and 5001.

One of these ports has HTTPS and the other one does not. Try opening a browser window and paste that address to see the outcome of the template.

Next we will create a new Users.cshtml file under Views — Home, and we will add another list item in the Views — Shared — _Layout.cshtml:

This tells our program to bind this item to the Home controller and the method Users.

We can now head into the Controllers folder and open the HomeController file:

As you can see, you need to add a new method which returns the view that we just created.

We will now need some data to display in this view, we will create an EntityFramework repository with an in memory database.

First, we need to run the following command to install EntityFramework to our project:

dotnet add package Microsoft.EntityFrameworkCore.InMemory

Now we will create a folder named Entities at the root of our project, and inside that folder we will create two files: User.cs and ApplicationDbContext.cs

The User class will be the entity that will be mapped to our temporary database, which will be accessed by ApplicationDbContext.

Now, we need to configure our DbContext in the ConfigureServices method in the Startup.cs file:

The line we want to add is number 11 in the above code snippet.

Now we want to add some seed data to our test database so that we can see something when we access out newly created users page:

As you can see, we overrided a method called OnModelCreating inside our ApplicationDbContext and added some test users.

Next we should create a UserViewModel inside our Models folder, this will be used to return data to our user view:

We now have everything ready to return data to our view, we will update our HomeController class to include a constructor, that will receive an ApplicationDbContext as a parameter, which will then be used by our Users method to fetch data from our temp database and return it to the view:

Pay close attention to the Users method, there we are calling EnsureCreated() to make sure our database is seeded with data, then we retrieve the records and add them to a List<UserViewModel>, which will then be returned to the view.

We need to build something to display our data, so we should go into the Users.cshtml file we created earlier.

I’ve created a really simple HTML structure combined with some Razor syntax to display our data:

Now, if we run our program by executing dotnet run in the terminal or by pressing F5 in VS Code, and we click on the Users button that is displayed in the navbar we should see our data displayed in a table:

As you can see, you can accomplish lots of powerful things when combining the different technologies that ASP.NET brings to the table. From here on out, you could add the functionality to perform CRUD operations with this table.

The Microsoft Docs are a great source of information that contain lots of examples on how to add this functionality.

Incorporating Docker

Let’s say you want to deploy this app that we just created to a Docker container.

We need to create a Dockerfile, this is what tells Docker how to configure the container for the app that you want to run.

Create a new file called “Dockerfile” at the root of the project and paste the following code inside it:

Let’s take a look at what the different commands in the Dockerfile mean:

FROM: Sets the baseImage to use for subsequent instructions. FROM must be the first instruction in a Dockerfile. We use dotnet:sdk as our base image.

WORKDIR: Sets the working directory for the subsequent Dockerfile commands.

COPY: Copies the specified files from the WORKDIR to the specified directory.

RUN: Executes the following commands at the current WORKDIR.

ENTRYPOINT: Configures the container to be run as an executable. We set it to use the app we just created, DockerTest.dll

Now that we have configured the Dockerfile, we have to build our image by executing the following command in the terminal:

> docker build -t dockertest .

Give Docker a minute to build your image, you can see the progress in the terminal:

Docker build process

Once the process is complete, you can check if your image was successfully built by typing docker images into the terminal:

Neat! We are now ready to run the Docker image and try our app, type the following into the terminal:

> docker run -d -p 8080:80 --name DockerTest dockertest

That’s it, your Docker image is now up and running. Head over to http://localhost:8080 and you should see your site:

You can check Docker processes by running docker ps in the terminal.

As you can see, deploying a Docker image is really easy.

Don’t forget that you can check the Github repo to find this whole project!

Pay attention to the underlined words in this article, there you will find very important links to information that will be very useful when you’re ready to move into more advanced topics regarding .NET and Docker!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store