Containerizing a .NET Application with Docker – Step by Step Guide

Containerizing a .NET Application with Docker – Step by Step Guide

Docker is a powerful tool that allows developers to package applications into containers. These containers can run on any system that has Docker installed, making it easier to deploy and run applications consistently across different environments. Containers are like lightweight, portable packages for your application, including everything it needs to run: code, runtime, libraries, and settings.

I assume that you have already know what Docker is and also you have .NET and Docker installed on your machine. Don’t worry, just download and then next + next. For more details and official documentation, you can visit Docker’s official website and for .NET visit here.

Building a Simple REST Web API in C#

Before we containerize an application, let’s create a simple REST Web API in C#. We’ll use the .NET CLI for this, which means you can follow along using any terminal, without relying on Visual Studio.

First, open your terminal and run the following command to create a new .NET Web API project:

dotnet new webapi -n MyDockerApp

Navigate to the Controllers directory in your project. If the directory does not exist, you should create it at the root level of your project. Inside this directory, create a new file named HomeController.cs.

using Microsoft.AspNetCore.Mvc;

namespace MyDockerApp.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Ok(new { message = "Hello from Home Controller!" });
        }
    }
}

Understanding the Role of Dockerfile

A Dockerfile is essentially a blueprint for building a Docker image. It contains a set of instructions that Docker uses to package your application into an image. This image includes everything your application needs to run, allowing it to be executed in any Docker environment. The Dockerfile specifies the base image to use, any dependencies that need to be installed, how to build your application, and what command to run when the container starts.

Now that we have our simple .NET app, let’s containerize it with Docker. We’ll create a Dockerfile in the root of our project with the following contents:

# Use the official Microsoft ASP.NET Core runtime for .NET 7.0
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80

# Use SDK Image to build our application
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["MyDockerApp.csproj", "./"]
RUN dotnet restore "./MyDockerApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "MyDockerApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyDockerApp.csproj" -c Release -o /app/publish

# Final image based on the base image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyDockerApp.dll"]

This Dockerfile does the following:

  • Starts with a base image from Microsoft that includes the .NET runtime.
  • Sets up an intermediate build image that includes the .NET SDK to build our application.
  • Copies our project files and restores any dependencies.
  • Builds and publishes the application to a directory in the container.
  • Sets up the final image, copies the built application from the build stage, and specifies the command to run the app.

Adding a Dockerfile to Your .NET Application

Navigate to the root directory of your .NET project where you want to add the Dockerfile. This should be the same directory that contains your project file (MyDockerApp.csproj in our example).

Create a new file named Dockerfile without any extension. With your Dockerfile open in your favourite text editor (notepad on Windows for example), you will need to input the instructions for building your Docker image. Refer to the Dockerfile content provided in the previous section of the guide. This includes specifying the base image, copying your project files, restoring dependencies, building your project, and setting the command to run your application within the container.

Running Your Dockerized .NET App

First, we need to build our Docke rimage. With the Dockerfile in place, you can now build and run your Docker container. Use the following commands in your terminal. This command tells Docker to build an image from your Dockerfile and tag it (-t) as mydockerapp.

However, make sure that Docker is running on your machine. Just go to Docker Desktop and run it.

docker build -t mydockerapp .

This will take a few minutes. It will download the image. Then we need to run it:

docker run -d -p 8080:80 --name myapp mydockerapp

This starts a new container (docker run) based on your mydockerapp image. The -d flag runs the container in detached mode (in the background), -p 8080:80 maps port 80 of the container to port 8080 on your host, allowing you to access your app at http://localhost:8080, and –name myapp names your running container myapp for easy reference.

Now, go to your browser and navigate to http://localhost:8080/home

Congratulations! You have just containerized your first .NET application with Docker. By following these steps, you can package any .NET application into a Docker container.

Suleyman Cabir Ataman, PhD

Sharing on social media:

Leave a Reply