Hosting ASP.NET Core 2.0 Application In Docker

In today's article, we will create an ASP.NET Core 2.0 MVC web application with Docker support and then, we will deploy our application in a Docker container (Windows 10).

Why do we need this?

The main purpose of Docker is to deploy applications to the production environment or other environments as required by Ops. However, the highlight of Docker lies in the process of deploying it.

Till Docker came into the picture, the traditional approach to deployment was via Virtual Machines (VM). And let’s not get started with the drawbacks of VMs because there are a plenty. Starting from the effort required to boot a VM, to launching multiple VMs in the same machine, it is a tough task to get things running with VMs.

So first, let’s see what does Docker provide?

Docker provides a container environment which can be used to host any application of your choice. The software application and the dependencies which support it are tightly-packaged together.

This packaged product is called a Container. Since it is done by Docker, it is called a Docker container!

Benefits of Docker container?

Since the application and its dependencies are packaged together, there is no external dependency for the app to run. This means the container is very light-weight.

Because it is light-weight, it can be easily shipped to other machines and be executed on those machines, irrespective of their host OS/ configurations.

The only requirement being, Docker has to be installed to run that application residing in the container.

So let's get started.

We will install docker for windows (we are using windows 10).

For that go to the following link - https://store.docker.com/editions/community/docker-ce-desktop-windows

You will need to login in order to download this. Once you have downloaded the setup and installed then restart your system.

After restarting go to command prompt and type docker version

You should see something like this in your console.

ASP.NET Core

It means that docker is successfully installed in your machine. Now we will create our ASP.NET Core 2.0 MVC application.

ASP.NET Core

ASP.NET Core

Make sure to check Enable Docker Support  checkbox and select your OS (mine is windows)

Once the project is created then you will see some docker files in the solution explorer,

ASP.NET Core

When we open Dockerfile,

ASP.NET Core

These are the contents of the auto-generated file.

What this file does is defines the dependencies which are required for our application and when we host our application in docker then this file is read by the docker to get the dependencies and get the entry point for our application.

So our application part is done. Now we will simply host our application in the docker container.

For that open command prompt and go to the project location where DockerFile resides and type docker-compose build and hit enter.

ASP.NET Core

You will see some processes, let them execute

After all the steps are done you should see something like this,

ASP.NET Core

After this to see the images type docker images and hit enter,

ASP.NET Core

You should see your application like this. So this is our image. But the image is just a template, we cannot connect to it, to run it we have to put this image inside a container and the run that container. So for that, we will copy its image id and type docker run your_image_id,

ASP.NET Core

As you can see now our application is running. So to open it in our browser we need IP address of our container which has the image. To check that type docker ps –a and hit enter,

ASP.NET Core

Here we can see our container that is just been created and now to get the IP address of our container type docker inspect your_container_id

It will give you information about your container like this

ASP.NET Core

You will get all of this information, now scroll down and look for NetworkSettings

ASP.NET Core

You will need this highlighted IP address.

There is another way to get this IP Address and that is to type the following

docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}"  your_container_id

ASP.NET Core

So now that we have got the IP address just enter this address in the browser.

ASP.NET Core

And here you can see our application running on a docker container.

Summary

In this article, we have seen how to deploy our ASP.NET Core 2.0 MVC web application into a docker container and run it through the IP address of the container rather than the localhost.

I hope you all liked it.