Containerization of the .NET Core 7 Web API using Docker

Introduction

In this article, we are going to discuss the step-by-step implementation and containerization of the .NET Core 7 Web API with the help of Docker.

.NET Docker

Agenda

  • What is Docker?
  • Why use Docker?
  • Benefits of Docker
  • Step-by-step implementation of the .NET Core Web API
  • Containerization of Applications

Prerequisites

  • Visual Studio 2022
  • Docker Desktop
  • .NET Core 7 SDK

What is Docker?

Docker is an open-source containerization platform that enables developers to build, run, and deploy applications quickly. Docker package application, including all its libraries, configurations, and dependencies.

Its primary focus is to automate the deployment of applications inside containers that boot up in seconds.

Why use Docker?

In the tech world, I think you've heard the phrase “It works on my machine,” and mostly, this happens because of the different libraries, configurations, and dependencies required for the application to run under the operating system.

Managing application dependencies and configuration is a crucial task for the DevOps team, and Docker has all the capabilities to handle this kind of problem in the software development lifecycle.

Docker helps us build and deploy distributed microservice applications with the help of continuous integration and a continuous deployment pipeline, which saves a lot of time.

Docker uses the container as a unit of software that packages application code with all its dependencies so the application can run quickly in isolated environments.

Benefits of Docker

  • Application portability: Docker is the container platform and allows running containers on a physical machine, virtual machine, or any other cloud provider in less time without modification
  • Faster delivery and deployment: Docker enables us to build and deploy application images across every step of the deployment phase efficiently.
  • Scalability: Docker is scalable because it has the ability to increase and decrease the number of application instances easily in different environments.
  • Isolation: Docker containerizes and runs the application in an isolated environment with all dependencies and configurations.
  • Security: Docker ensures the applications running inside the different containers are isolated from each other, and it has different security layers and tools to manage that.
  • High Performance: Docker is generally faster and takes fewer resources than VMs.

Version Control Management: Docker provides versioning-related things that can track container versions and roll back the same if required.

Step-by-step implementation of the .NET Core Web API

Let’s create one Web API application.

Step 1. Create a new .NET Core Web API application.

Step 2. Configure your new project.

Step 3. Provide additional information.

Step 4. Install the following packages.

Step 5. Add WeatherForecast.cs to a new project.

namespace NET_Core_Web_API_Docker_Demo
{
    public class WeatherForecast
    {
        public DateOnly Date { get; set; }

        public int TemperatureC { get; set; }

        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

        public string? Summary { get; set; }
    }
}

Step 6. Next, Create a new WeatherForecastController.

using Microsoft.AspNetCore.Mvc;

namespace NET_Core_Web_API_Docker_Demo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

Step 7. Update a Program.cs file.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();

app.UseAuthorization();

app.MapControllers();

app.Run();

Step 8. Run the newly created application.

Containerization of Applications

Note. Please make sure Docker is running on your system.

Step 1. Create a docker image for our newly created application.

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

# Copy the project file and restore any dependencies (use .csproj for the project name)
COPY *.csproj ./
RUN dotnet restore

# Copy the rest of the application code
COPY . .

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

# Build the runtime image
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS runtime
WORKDIR /app
COPY --from=build /app/out ./

# Expose the port your application will run on
EXPOSE 80

# Start the application
ENTRYPOINT ["dotnet", "NET-Core-Web-API-Docker-Demo.dll"]

Explanation

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

Start with the base image for your Docker container, which is specified as mcr.microsoft.com/dotnet/sdk:7.0. This image uses the official.NET Core SDK 7.0 as its starting point. To later refer to this stage it is given the alias “build”.

Next, set the working directory within the container to /app. This is where your application files will be copied and built.

# Copy the project file and restore any dependencies (use .csproj for the project name)
COPY *.csproj ./
RUN dotnet restore

To copy the project files (*.csproj) from your computer to the /app directory in the container, you need to use the command “COPY *.csproj./”. This step is separated from the one to optimize Docker’s layer caching. It ensures that when there are changes in your project file, the subsequent steps will be executed.

To restore the project’s dependencies and make sure that all required packages are downloaded and available for building the application, you can use the command “RUN dotnet restore”. This command will take care of restoring all dependencies.

# Copy the rest of the application code
COPY . .

Next, Copy the remaining source code and files from the local directory to the app directory and build the application

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

“RUN dotnet publish -c Release -o out” This command restores project dependencies and all required packages.

This command builds the .NET Core Web API Application in Release mode and publishes the output to the out directory. The -o out flag specifies the output directory.

# Build the runtime image
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS runtime
WORKDIR /app
COPY --from=build /app/out ./

FROM mcr.microsoft.com/dotnet/runtime:7.0: This section starts a new phase using the runtime image. This image is small and contains only the runtime needed to run .NET Core applications, unlike the SDK image, which is used for builds.

WORKDIR /app: Set the working directory as /app in this new section.

COPY — from=build /app/out .: Copies the compiled application from the build component to the current runtime component. This ensures that only necessary features are added to the final runtime image.

# Expose the port your application will run on
EXPOSE 80

EXPOSE 80: This line specifies that the container will expose port 80.

# Start the application
ENTRYPOINT ["dotnet", "NET-Core-Web-API-Docker-Demo.dll"]

ENTRYPOINT [“dotnet”, “NET-Core-Web-API-Docker-Demo.dll”]: Specifies the command to be executed when an object based on this image is started. In this case, it creates your .NET Core Web API Application by calling the dotnet NET-Core-Web-API-Docker-Demo.dll.

Step 2. Build the docker image.

docker build -t web-api-demo.

The docker build command is used to build a Docker image from a Docker file. It includes a variety of options, including the -t option to specify a tag for an image.

This command creates a Docker image that uses the Dockerfile in the current directory (.) and marks it as web-api-demo.

Step 3. Run the docker image inside a docker container.

docker run -d -p 5001:80 — name web-api-container web-api-demo

  • -d: Detached mode (run in the background).
  • -p 5001:80: Map port 5001 on your local machine to port 80 inside the container.
  •  — name web-api-container: Assign a name to the container.
  • web-api-demo: Use the image you built earlier.

Step 4. Open the browser and hit the API URL to execute different endpoints.

GitHub

Conclusion

In this article, we discussed about basics of docker and step-by-step implementation of the .NET Core 7 Web API application and containerization with the help of docker.


Similar Articles