Tech Blog

Technology

Dockerize a .Net core application for Beginners

By Sai Nandhan3 min read
← Back to blog

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!

Keep reading

You may be interested in

Browse all articles →
Using SCEP Certificates with Okta and SureMDM for Zero Trust Security
August 5, 20257 min read

Using SCEP Certificates with Okta and SureMDM for Zero Trust Security

For businesses embracing zero-trust principles, secure certificate-based authentication isn’t just nice to have—it’s a necessity. With Okta as your Identity Provider (IdP) and SureMDM as your unified endpoint management solution, you can now seamlessly distribute and manage device certificates using Simple Certificate Enrollment Protocol (SCEP). This blog will guide you through: Why Use SCEP with […]

By Sunny KumarRead More →
How to Use mc CLI for Bucket Management and Credential Setup
June 12, 20252 min read

How to Use mc CLI for Bucket Management and Credential Setup

MinIO client The MinIO Client, commonly known as mc, is a command-line tool used to interact with MinIO, a high-performance object storage system. The mc client allows users to manage buckets and objects on the MinIO storage service.

By Malkeet SinghRead More →
How to Setup Passwordless SSH Login for ADE
March 18, 20253 min read

How to Setup Passwordless SSH Login for ADE

To set up a passwordless SSH login in Linux all you need to do is to generate a public authentication key and append it to the remote hosts ~/.ssh/authorized_keys file. The following steps will describe the process for configuring passwordless SSH login: 1. Check for existing SSH key pair Before generating a new SSH key […]

By Mohan Kumar KMRead More →