Docker helps us develop, run, and ship apps anywhere. In this blog post, we will explore what is docker and how it can be added to a project using visual studio code.
What is Docker?
Docker is a container management service. It is written in Go and open source. Docker container enables developers to bundle application and it’s dependencies and ship everything out as one package which then can be deployed.
Container is a just a process which shares the host system kernel. It is limited to what resources it can access and stops when process stops. It is very small than a virtual machine which makes it very light and fast to start and stop.
You can download the docker from docker.com for windows/linux based on your machine’s OS.
You should become familiar with some of the following terms and definitions
Container Image: A set of all the dependencies and information needed to create a container.
Container: A container describes a runtime for a system, process, or service. It is an instance of a docker image.
Tag: Tag is used to label images to identify different versions and environments.
Dockerfile: It is a text file that holds commands on how to build a docker image.
Compose: YAML file format with metadata for creating and running multi-container applications. It is a very handy CLI tool that let’s us manage the multi-containers with single commands.
Dockerize ASP.NET Core Application
.Net core works on Linux and windows both. To containerize .net core application add the Dockerfile in your project folder.
Dockerfile should be in the directory of your project. If you have referenced other projects. You need to place Dockerfile in the right context.
In my project, I have placed Dockerfile in the Solution folder because I need to copy my multi-layers API, domain, service, data DLL’s.
A Dockerfile is a text file that holds commands on how to build a docker image. It can contain multi-stage builds. The following file has four build stages.
The base stage gets ASP .net core runtime and NuGet packages from the docker hub. It then sets the working directory to the ‘app’. Exposes docker’s port 80 and 443 for network communication with the outside world.
The build stage gets the SDK image to run the application. It will set the working directory to ‘src’, copy all the project files, and store DLLs inside this folder. Next, it will restore the NuGet packages of User.API project.
Copy . . is a very important command. It copies all the files from your docker build context(current project directory) to the docker image directory.
Publish stage publishes the application to /app/publish directory. The parameter ‘-c Release -o /app/publish’ compiles the app with release configuration to the output path.
In the final stage, we set the path back to ‘/app’ because it is where our app is going to run with a runtime environment (see Stage 1). Copy all the files from /app/publish directory to /app.
When the docker container starts. ENTRYPOINT will execute the ‘Dotnet project_name.dll’ command. In our case, it is bound to run the application with the help of User.API.dll.
Build and Run Docker Image
Now we have to build an image and run that image in the container. Go to the project directory where the Dockerfile exists and run the following command
docker build -t user.api .
-t is optional and used to tag images. “.” tells the docker build command to set the build context to the current directory of the project.
The following command will run the image in container.
docker run -p 3000:80 –name userapi-container user.api
With -p (–publish) option we bind the container’s port to the host (using TCP). With the –name option we specify the name of the container. At the end we pass the name of the image.
In this post, I walked you through about core concepts of Docker and how it can be added to .Net core. In a fast DevOps culture, it is required to quickly change the application and deploy it in production. Docker makes this very easy.
In the next post, I will explain how you can use powerful commands like docker-compose to deploy multiple services with a single command.
You can download the reference source code on my Github.