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.
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.
Dockerfile contains all the dependencies and information to run the application. Below is the content of docker file .
- #This is base image of dotnet core application which is created by Microsoft.
- FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
- WORKDI /app
- # Copy csproj and restore its dependencies such as NuGet packages
- COPY *.csproj ./
- RUN dotnet restore
- # Copy complete project files and build
- COPY . ./
- RUN dotnet publish -c Release -o out
- # Build runtime image
- FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
- WORKDIR /app
- COPY --from=build-env /app/out .
- 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.