Containerize .NET Core Application Using Docker

In this article, I am going to explain the concept of containers and demonstrate how to run your dotnet core web application in a container.
 
Containerize .NET Core Application Using Docker
 

What Are Containers and Why Use Them

 
It is a common scenario in which  code is working fine on a developer’s machine, however it doesn’t work when deployed on a different machine. To solve such issues related to environmental dependencies, containers come into the picture.
 
You can visualize containers using the following example to understand them in a better way.
 
In earlier days, it was very difficult to transport different goods from one place to another using the same methods. Transportation and packaging was totally dependent on the things which you were going to transport. For example, some goods could not be transported through the sea route or via roads. To solve this problem, the invention of containers was a breakthrough. Containers can be packaged with any type of material and product . The respective environment for these product can be provided or created in the container itself.
 
So It became very easy to transport any type of product through any media.
 
In the same way, Containers encapsulate code and all its dependencies so the application runs quickly and reliably from one environment to another without the hassle of environment-related dependencies.
 
There are multiple vendors available which provide containers and among those Docker is the most popular. In this example we are going to use Docker.
 
Steps for creating and running the dotnet core app in container:
 
Create a web application named “containerapp” using the below command
 
“dotnet new webapp -o containerapp”
 
Create a file in the project root directory “Dockerfile”. If you are using Visual Studio you can right click on the project -> Add new item and add Docker support. It will automatically create a docker file and docker ignores the file for you.
 
Containerize .NET Core Application Using Docker
 
Dockerfile contains all the dependencies and information to run the application. Below is the content of docker file .
  1. #This is base image of dotnet core application which is created by Microsoft.    
  2. FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env   
  3. WORKDI /app    
  4. # Copy csproj and restore its dependencies such as NuGet packages    
  5. COPY *.csproj ./    
  6. RUN dotnet restore    
  7. # Copy complete project files and build    
  8. COPY . ./    
  9. RUN dotnet publish -c Release -o out    
  10. # Build runtime image    
  11. FROM mcr.microsoft.com/dotnet/core/aspnet:2.2    
  12. WORKDIR /app    
  13. COPY --from=build-env /app/out .    
  14. ENTRYPOINT ["dotnet""containerapp.dll""http://*:5000"]     
Build image and run container
  • Command to create an image

    docker build -t containerapp .

    • container app is the name of image
    • . is for docker file directory location, in this case It is the same directory.
    • This command will pull all the required images mentioned in docker file
After successful execution of the above command you can check your image by using command “docker images”. It will list all the docker images.

Once the image is successfully created you can run the container by the following command:

“docker run -d -p 5000:80 --name demo_container containerapp”
  • -d means to run the container in background
  • -p means map the container port like external port: internal port
  • --name is the container i.e. demo_container
  • Last argument is image name
“docker ps” command is to list out all running containers.
 
If you want to stop or remove your containers and remove images below are the commands to do that
  • “docker stop containername” to stop container
  • “docker rm containername” to remove container
  • “docker rmi imagename” to remove image
You can download the complete code at the following link – Download code.
 

Conclusion

 
I hope this article will help you to understand the basics of container in the context of dotnet core applications. It is a great tool which supports the continuous deployment pipeline. It is growing and offers many use cases which might be helpful for you. In this way you can containerize any type of application in dotnet core such as console and REST API.


Similar Articles