Introduction to Docker for .NET Developers


In this article, I will discuss the basics of Docker containerized system which every Developer should know. I will try to make this article as concise as possible and focus on the core topics.

What is Containerized System?

First, you should know what is Container? The best way to understand container is to picture the Shipping Container.

Shipping Container allows various kinds of goods to be transported by different transportation medium like train, ship or truck. It makes an abstraction on the product and let think about the shipping only. Containers can be placed one after another and make it manageable and scalable.

Similarly, software containers enable developers to deploy an application across environments with little or no modification.

A containerization is a software development approach where application or service, its dependencies and its configuration (abstracted as deployment manifest files) are packaged together as a container image.

Containerized application provides a lot of facilities to make application scalable and distributable.

  • This containerized application can be deployed as an instance to the host OS.
  • All of the software parts and dependencies are packaged together and act as a unit of software. It helps on thinking about the application design and architecture without focusing on the infrastructure (cloud or on premises) of your production environment. You can make decisions on your infrastructure later when you create the production-ready applications.
  • Software container also helps on scalable software development. New containers can be added on demand.
  • Also, reliability can be ensured by running multiple instances of the same image across multiple servers.

Why Docker?

Docker is a tool which makes the developers life easier by automating the deployment of applications using containers. Docker image containers include the sufficient information and make the application portable and isolated which is needed to run the application on the cloud or on-premises.

Docker Image

Docker can build images automatically by reading the instructions from a Dockerfile a text file that contains all the commands, in order, needed to build a given image.

Docker image is a layered system and the created image inherits the Base image. It refers to the contents of the FROM directive in the Dockerfile. Each subsequent declaration in the Dockerfile modifies this base image.

Docker layered image architecture makes the base image shareable which minimizes the storage cost. Again, containers are Ephemerals which means it can be stopped and destroyed and a new one built and put in place with an absolute minimum of set-up and configuration, the container’s data are not persistent.

Docker architecture

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of the building, running, and distributing your Docker containers.

Docker Architecture Diagram

  • The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.
  • When you use commands such as docker run, the Docker client (docker) sends these commands to dockerd, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.
  • A Docker registry stores Docker images. When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry.

Docker Setup

Install docker for your workstation from the official Docker distribution page. 

To verify the installation try the following command:

$ docker --version

This command will output the installed Docker version in your workstation.

Common Docker Commands

Pull image from Registry

$ docker pull debian
Using default tag: latest 
latest: Pulling from library/debian 
fdd5d7827f33: Pull complete 
a3ed95caeb02: Pull complete 
Digest: sha256:e7d38b3517548a1c71e41bffe9c8ae6d6d29546ce46bf62159837aad072c90aa 
Status: Downloaded newer image for debian:latest

This command will pull the Debian image. If no tag is provided, Docker Engine uses the :latest tag as a default.

As we know, Docker images can consist of multiple layers. In the example above, the image consists of two layers; fdd5d7827f33 and a3ed95caeb02

List pulled images

$ docker images

Show Docker containers

$ docker ps

Show only running containers by default.  To all containers including the exited containers use -a option

$ docker ps --filter "name=nostalgic_stallman"

Show all containers with a name containing the nostalgic_stallman string.

Remove containers from docker host

$ docker rm

Remove Docker image from docker host

$ docker rmi

Stop All Containers

$ docker stop (docker ps -a -q)

Remove All Containers

$ docker rm (docker ps -a -q)

Steps on Creating ASP.NET Core Container Image for Docker

In this section, we will create ASP.NET Core application with .NET Core runtime and make our first Docker image using Dockerfile. 

1. Create .NET Core Application

Run the below command to create an ASP.NET Core MVC application named aspnetcore-docker 

$ dotnet new mvc -o aspnetcore-docker

2. Run the ASP.NET Core app

Run the application in local host

$ cd aspnetcore-docker
$ dotnet restore
$ dotnet run

Now the application starts listening on port 5000. http://localhost:5000 

3. Publish the ASP.NET Core app

Now, publish the app to get a self-contained DLL using the dotnet publish command.

$ dotnet publish -o ./release

Running publish displays some messages with a successfully published DLL at the end of the process.

...
aspnetcore-docker -> /Users/mainulislam/Desktop/Projects/aspnetcore-docker/bin/Release/netcoreapp1.1/aspnetcore-docker.dll

You should now have a publish folder, that contains your compiled application.

4. Package the ASP.NET Core app as a Docker container

Create a Dockerfile at the root of for your application

$ touch Dockerfile

The following example assumes you have already compiled your application like the previous step. Add the following to Dockerfile

FROM microsoft/aspnetcore:1.1.2
WORKDIR /app
COPY ./release .
ENTRYPOINT ["dotnet", "aspnetcore-docker.dll"]

This Docker image will copy the contents of the publish folder in the root of your project into the app folder on the image.

Microsoft provides two different images for aspnetcore

  • microsoft/aspnetcore-build which is used during development as it contains the element in order to compile your app. This includes the compiler and any other .NET dependencies and other web development dependencies like npm, Gulp, Bower. This image will also be used in the continuous environment (CI).
  • microsoft/aspnetcore is a smaller image file and also used for production deployment. This is a runtime only image and optimized for production

5. Build the Docker image

docker build -t aspnetcore-docker .

6. Test the image

docker run -p 8080:80 aspnetcore-docker

Run Docker ImageNow you can navigate to the hosted application: http://localhost:8080/

Docker Container Run

7. Publishing & Pulling Images

Now that we build the image and now we can use that on other docker hosts. To do so we can upload our image to a Docker registry. There are many different choices for Docker registries: hub.docker.com, AWS ECR, Artifactory…

We will be using hub.docker.com which is free for public images. If you haven’t done so yet, create an account. You can then login from Terminal:

docker login

Docker login

To be able to upload (push) our image we have to prefix our image with our username. My username is mainul98 so I would have to run the following command:

docker tag aspnetcore-docker mainul98/aspnetcore-docker

Now I can push the image

docker push mainul98/aspnetcore-docker

Push ImageAfter the image is pushed I can verify that it worked by opening the following URL: https://hub.docker.com/r/mainul98/aspnetcore-docker/

To pull the image run the following command now you can also run my image like this:

docker run -p 8081:80 mainul98/aspnetcore-docker

Docker pull

My image will now be pulled and run on your machine. Check it out under http://localhost:8081 (Changed port to 8081)

That’s all for the basic of Docker for .NET developers. Please share your valuable opinion. Happy coding ♥

 

2 responses to “Introduction to Docker for .NET Developers”

  1. Very good article and nice writing. Keep up the good work.

  2. Nice article and easy to follow the steps.

Leave a comment