Dockerize a .Net core application for Beginners


Hi Everyone!! If you have a .Net core web application and you are looking for a way to dockerize it. This article is for you. In this blog, I will explain how to dockerize a .Net core application from scratch. Once it is dockerized, you can run it as a docker container or as a pod in kubernetes.

Prerequisites:

1)docker for desktop
You can install docker for desktop for windows from the following link.
https://docs.docker.com/docker-for-windows/install/

Introduction:

Firstly, in order to create any docker image, we need to define one file called the Dockerfile. This Dockerfile basically defines what and all needs to be present in the docker image.

A sample Dockerfile for .net core would look like this,

#The first line of dockerfile is always a base image. In this case, since it is a dotnet core application, we #use the microsoft’s official dotnet core sdk base image. You can mention either specific version or the the #tag ‘latest’, Everytime we use a base image, that is considered as a stage. Now we are naming this stage #as ‘build’
FROM mcr.microsoft.com/dotnet/core/sdk:3.1.404 AS build
#This sets the current virtual working directory to ‘/app’
WORKDIR /app
#This ‘COPY’ command copies all the files & folders from local to docker server.
COPY ./ ./
#Switching the current directory to ‘/app/<your application name>’
WORKDIR /app/Application1
#this command restores all the dependencies needed for your project.
RUN dotnet restore
#This command publishes and gives all dll files in the ‘out’ folder in the same directory.
RUN dotnet publish -c Release -o out

#For a dotnet application to run, it requires a dotnet runtime environment. So, we use a aspnet base docker #image. Here, we are naming the stage as ‘runtime’
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.10 AS runtime
#If your .net core application is using sql server, then you need to install few odbc drivers and mssql #libraries. The following command installs all those libraries.
RUN apt-get update && \
apt-get install -y gnupg2 && \
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add – && \
curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/
mssql-release.list && \
apt-get update && \
apt install -y libodbc1 && \
apt-get install -y libgdiplus && \
ACCEPT_EULA=Y apt-get install -y msodbcsql17 && \
ACCEPT_EULA=Y apt-get install -y mssql-tools && \
echo ‘export PATH=”$PATH:/opt/mssql-tools/bin”‘ >> ~/.bash_profile && \
echo ‘export PATH=”$PATH:/opt/mssql-tools/bin”‘ >> ~/.bashrc && \
apt-get install -y unixodbc-dev && \
apt-get install -y libgssapi-krb5-2
WORKDIR /app
#Now, all our project dll files are present in 1st stage(build). but we are in the 2nd stage. So, we need to copy all the dll files present in the ‘out’ folder from the ‘build’ stage to the current directory in the ‘runtime’ stage.
COPY –from=build /app/Application1/out ./
#Entrypoint is used to run the given command once the container starts. You can change the port number #based on your application needs
ENTRYPOINT [“dotnet”, “Application1.dll”, “–url=’http://localhost:80′”]
EXPOSE 80

This is how we define a ‘dockerfile’. You need to save the file as ‘Dockerfile’ with no extension.

Then you need to run the following command to create a docker image. Make sure you run the command from the directory where all the project folders are present.

docker build -t application1:sprint . -f Dockerfile

‘application1’ is the docker image name that we gave and ‘sprint’ is the tag.

Conclusion:
In this blog, I have explained how to dockerize your .net application. In the similar way, you can dockerize any .net core application with few changes as per your requirement.
I hope you enjoyed the article!

Leave A Comment

Your email address will not be published. Required fields are marked *