Docker and .NET: A Comprehensive Tutorial

Welcome to this in-depth tutorial on integrating Docker with your .NET applications. This guide will walk you through the process of containerizing your .NET projects, enabling you to build, deploy, and run applications consistently across different environments.

Introduction

Docker is a platform that enables developers to package applications into containers – isolated environments that include everything needed to run the application: code, runtime, system tools, system libraries, and settings. This ensures that your .NET applications run reliably regardless of the underlying infrastructure.

In this tutorial, you will learn:

Prerequisites

Before you begin, ensure you have the following installed:

Creating a Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. We'll start by creating a simple ASP.NET Core web application and then add a Dockerfile.

Step 1: Create a new ASP.NET Core Web Application

Open your terminal or command prompt and run the following commands:

dotnet new webapp -o MyWebApp
cd MyWebApp

Step 2: Add a Dockerfile

Create a new file named Dockerfile (no extension) in the root directory of your project (MyWebApp). Add the following content:

Note: This example uses the official Microsoft .NET SDK image as a base. Choose the appropriate SDK version for your project.

# Use the official .NET SDK image as a build environment
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app

# Copy the solution file and restore dependencies
COPY *.sln ./
COPY MyWebApp/*.csproj ./MyWebApp/
RUN dotnet restore

# Copy the rest of the application code
COPY MyWebApp/ ./MyWebApp/
WORKDIR /app/MyWebApp

# Publish the application
RUN dotnet publish -c Release -o /app/publish

# Use the official .NET runtime image for the final image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app

# Copy the published output from the build stage
COPY --from=build /app/publish .

# Expose the port the application listens on
EXPOSE 8080

# Define the entry point for the container
ENTRYPOINT ["dotnet", "MyWebApp.dll"]

Building the Docker Image

Navigate to the root directory of your project in the terminal (where the Dockerfile is located) and build the Docker image using the following command:

docker build -t my-dotnet-webapp .

This command:

Running the Container

Once the image is built, you can run it as a container:

docker run -p 8080:8080 my-dotnet-webapp

This command:

You can now open your web browser and navigate to http://localhost:8080 to see your .NET application running in a Docker container.

Using Docker Compose

For applications that involve multiple services (e.g., a web API and a database), Docker Compose is an invaluable tool. It allows you to define and manage multi-container Docker applications.

Step 1: Create a docker-compose.yml file

In the root of your project, create a file named docker-compose.yml with the following content:

version: '3.8'

services:
  webapp:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      ASPNETCORE_URLS: http://+:8080
    depends_on:
      - database # If you had a database service
    # Add other configurations like volumes, networks if needed

  # Example database service (e.g., PostgreSQL)
  # database:
  #   image: postgres:15
  #   environment:
  #     POSTGRES_DB: mydatabase
  #     POSTGRES_USER: user
  #     POSTGRES_PASSWORD: password
  #   ports:
  #     - "5432:5432"
  #   volumes:
  #     - db_data:/var/lib/postgresql/data

# volumes:
#   db_data:

Step 2: Run with Docker Compose

From your project's root directory, run:

docker-compose up

This command will build your image (if not already built) and start the container(s) defined in the docker-compose.yml file.

Advanced Topics

This tutorial provides a solid foundation for using Docker with .NET. Explore the official Docker and .NET documentation for more advanced techniques and best practices.

Windows macOS Linux

This guide is applicable across all major operating systems where .NET and Docker are supported.

^