ASP.NET Core apps in Docker

docker

Docker

Docker is an open-source containerization platform for building software. In Docker, an app is put to a Container along with all the dependencies of the software, these are packed together as a Container Image. So, in short it means that Docker contains Containers and containers contain Images.

So how does it relate to ASP.NET Core App?

Suppose you have an ASP.NET Core App which you want to run on Docker. You will achieve this by putting your app along with Dot Net Core SDK (which is a dependency of your app) as an Image to the Container. So now your ASP.NET Core App will run from the container, it will not need:

  • 1. Dot NET core installation on your PC.
  • 2. Operating system in your pc as docker is itself an operating system for the container.
  • 3. Any other dependency that the app has, as everything is inside the container image.

That sounds great, isn’t it?

In the below image I have explained by showing 3 images contained by 3 containers of docker. These images can be any program with all it’s dependencies, and executes directly from the container without needing anything else.

docker containers images

Docker Benefits for Developers

Docker provides a lot of benefits to developers. Some important ones are:

  • 1. Fast, easy and consistent delivery of apps.
  • 2. Responsive development and scaling of apps.
  • 3. Reduces cost of development of apps.

Fast, easy and consistent delivery of apps by using Docker

Since apps kept inside the containers can run on their own therefore developers can work directly on these containers. This comes out to be handy for continuous integration and continuous delivery (CI/CD) workflows.

Let us understand it with a simple example. Two people in your organization, Developer A who lives in Seattle & Tester B who lives in New York, are developing as ASP.NET Core APP. This app does a simple task of providing the current value of a stock listed on Nasdaq. The app contains a single page where users can enter a stock name (example Microsoft) and submit it by clicking a button. The app then calls an external REST API service which will return back the current price of the stock.

So, what happens here is, first Developer A develops this app in a Docker Container, and ships this container to Tester B. The shipment is not done physically through Post office or Fedex, as the containers are virtual in nature, so instead it can be done through internet just as emails are send.

Fast development of apps with docker

Fast Development of Apps with Docker

Now Tester B, on receiving this container, runs the app from the container itself and does some tests. If the tests are failed then developer A has to fix them. When all the tests passe then the app becomes ready for deployment on production server.

Easy development of apps with docker

Easy Development of Apps with Docker

Finally, developer A puts the container on a cloud service provider like Azure or AWS or Google Cloud, and the app becomes live for people around the world. This is as simple as making a tea by yourself.

Consistent development of apps with docker

Consistent Development of Apps with Docker

Responsive development and scaling of apps with Docker

Docker containers can run anywhere, like on laptop, on a virtual machine, on a data center or on a cloud provider. It is also very easy for scaling up the app or tearing it down.

Take for example, an app running inside 5 containers suddenly becomes very popular and start getting huge traffic. So, we can increase the containers to 10 to fulfil its needs. In the same way we can reduce the containers from 5 to just 1 in-case if the traffic to the app becomes less.

docker app scaling

Docker App Scaling

Reduces cost of development of apps by using Docker

Docker is lightweight and fast, it is a cost-effective alternative to hypervisor-based virtual machines and so will save your money. You also don’t need to install paid software’s like SQL Server on your pc as they can run from a docker container itself.

For example, I can download SQL Server image from internet and put it inside a docker container. Now my SQL Server will run from a container itself and I will not need to install it on my pc. This saves me from buying it’s licence fee.

sql server docker container

SQL Server Docker Container

Docker Architecture

Docker architecture is Client-Server type, there is a Docker Client component, which is also known as Docker CLI, and Docker Server component. The Docker Server is also called by another name Docker daemon (dockerd). We as users uses docker client to communicate with daemon to do tasks like creating containers with images, pulling and pushing images, and so on.

The Docker daemon does the heavy lifting tasks like managing images, containers, networks, and volumes. When we install docker on our local PC then both the Docker client and daemon gets installed on our local PC.

When deploying apps on Azure we use our docker client installed in our PC to connect with the daemon installed on Azure server and instruct it do perform operations for us.

Image, Container & Registries in Docker

A Docker Image contains the app along with it’s dependencies. Often these images are based on other images like we can have a ubuntu OS image where IIS and dot net SDKs are installed. Then we also have our ASP.NET Core app in that image to.

An image is contained by a Docker Container. You can create, start, stop, move, or delete a container using commands send from the docker client to daemon. As already discussed, the container is self-sustained, that is – the app kept in the image can run on it’s own without needing anything else from the outside world.

A Docker Registry stores docker images. We can create a Docker Image on our local pc and then push the image on a docker registry. Now, once an image is stored in the docker registry then other people can pull that image from there, into their own local pc, and create a docker container containing that image. Do you remember the container delivery example (post office, Fedex) I gave earlier? The Docker Registry fits there and the delivery or the container (or rightly say the delivery of the Image) is powered by it.

The 2 famous Docker Registry are:

Docker Hub contains millions of docker images which you can pull to your local pc and use them to create great apps.

In the below given image I have illustrated the working of docker registry.

docker registry

Docker Registry

In the End

Docker is a great way to build app and you are going to love this platform. In the coming series of tutorials, I will be teaching you docker from start till end. You will be learning how to create ASP.NET Core app inside Docker Containers and publish them on Azure. So do check these tutorials which are listed below.

ASP.NET Core apps in Docker Tutorials with DEMOS and CODES for Download