An Easy Guide to Dockerize a .NET Application
Written By harish
Last Updated: July 5, 2024
July 3, 2024

Want to receive our Blog every month?


This guide provides a step-by-step guide for dockerizing a .NET project . Dockerizing ensures consistent dependency versions and effortless cross-environment compatibility. It simplifies development and deployment processes significantly.


  • .NET SDK: To build and run .NET applications.
  • Docker Environment: Ensure your development machine has Docker Desktop or the command-line equivalent installed. Refer to the official Docker documentation for installation instructions:
  • A basic understanding of Docker concepts.

Step by Step Guide

Dockerizing an .NET application involves a series of steps to containerize the application effectively. Below are guidelines to help you dockerize a .NET application:

Step 1: Create a Docker File

In the Root Directory of the project create a Dockerfile , it contain the instructions for building a Docker image for the project

Here is the well-structured dockerfile template , you can customize this based on the project requirement

# Use the official .NET SDK image for building
FROM AS build

# Expose port 5000 (adjust if your application uses a different port)

# Copy the project file and restore dependencies
COPY . ./
RUN dotnet restore

# Copy the remaining source code
COPY . ./

# Build the application
RUN dotnet publish -c Release -o out

# Install EF Core tools extension
RUN dotnet tool install --global dotnet-ef --version 8.0.0
ENV PATH $PATH:/root/.dotnet/tools

# Build runtime image
FROM AS runtime
COPY --from=build /app/out ./
ENTRYPOINT ["dotnet", "YourAppName.dll"]

We can also create a Dockerignore file in the root directory to excluded unnecessary files and directories from being copied into the Docker image. This helps in reducing the image size


Step 2: Docker Compose File

Create a docker-compose.yml file if you want to define services, networks, and volumes for your docker environment.

Here we have defined a MariaDB database with backend in .NET along with a volume to store data

version: "3"
    image: mariadb:11.2.2
    container_name: foundation-mariadb
      - '' # Bind port to localhost to enhance security
      - .env
      - foundation-mariadb-db-data:/var/lib/mysql
    restart: always

      context: .
      dockerfile: Dockerfile
      - foundation-mariadb
    image: foundation-backend
    container_name: foundation-backend
      - '' # Bind port to localhost to enhance security
      - DOTNET_URLS=http://+:5000

    driver: local

Step 3: Build Docker Image

Run the below mentioned command to build the docker image

docker-compose build

Step 4: Run Docker Container

Once the image is build we can use the below mentioned command to run the docker image

docker-compose up -d

Here the -d flag is used to start the container in detached mode

Verify the status of the container by using below mentioned command

docker container ls

Step 5: Test the Container

Access your application in a web browser or using a tool like curl to ensure it’s running properly.

curl http://localhost:5000


In this guide, we’ve covered the process of dockerizing a .NET modern application, specifically a Web API application with a database, by configuring them in separate containers. By following these steps, you can leverage the power of Docker to streamline your development and deployment process.

Benefits of Docker

  • Isolation and Consistency: Docker ensures consistent environments and isolates dependencies.
  • Portability: Docker containers are portable across different platforms.
  • Simplified Deployment: It simplifies the deployment process and reduces compatibility issues.

Keep up to date with Osmosys Blog!