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?

Introduction

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.

Prerequisites

  • .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: https://docs.docker.com/get-docker/
  • 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 mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app

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

# 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 mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
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

bin/
obj/

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"
services:
  foundation-mariadb:
    image: mariadb:11.2.2
    container_name: foundation-mariadb
    ports:
      - '127.0.0.1:3307:3306' # Bind port to localhost to enhance security
    env_file:
      - .env
    environment:
      - MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
      - MYSQL_USER=${MARIADB_USER}
      - MYSQL_PASSWORD=${MARIADB_PASSWORD}
      - MYSQL_DATABASE=${MARIADB_DATABASE}
    volumes:
      - foundation-mariadb-db-data:/var/lib/mysql
    restart: always

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

volumes:
  foundation-db-data:
    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

Conclusion

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!