How to Use Docker with ASP.NET Web API

By Henri Parviainen

Docker with dotnet

Introduction

Docker has become a fundamental tool in modern software development, enabling developers to package applications and their dependencies into containers. This allows for consistency across development, testing, and production environments. ASP.NET Web API is a popular framework for building web services in the .NET ecosystem. In this article, we'll explore how to use Docker with ASP.NET Web API to streamline the development, testing, and deployment processes.

Prerequisites

Before we dive into using Docker with ASP.NET Web API, make sure you have the following prerequisites in place:

  1. Docker Installed: You need to have Docker installed on your development machine. You can download and install Docker Desktop from the official Docker website.
  2. .NET SDK: You should have the .NET SDK installed on your machine, which can be downloaded from the .NET website.

Create an ASP.NET Web API Project

To get started, create a new ASP.NET Web API project using the dotnet new webapi -o myWebApi command.

This will create a new ASP.NET Web API project in the myWebApi directory.

You can name the project however you want. For this tutorial, we will use myWebApi.

Next, we will need to create a Dockerfile for the project.

Create a Dockerfile

A Dockerfile is a configuration file that defines the image for your Docker container. In your ASP.NET Web API project directory, create a file named Dockerfile without any file extension.

In your Dockerfile copy the following contents:

# Use an official .NET runtime as a base image
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base

# Set the working directory in the container to /app
WORKDIR /app

# Expose ports 80 and 443 in the container
EXPOSE 80
EXPOSE 443

# Pull the .NET 7.0 SDK image from Microsofts container registry
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build

# Set the working directory in the container to /src
WORKDIR /src

# Copy the source code from the host machine to the container
COPY ["myWebApi.csproj", "./"]
# Restore Nuget packages for the project
RUN dotnet restore "./myWebApi.csproj"
# Copy the rest of your application code to the container
COPY . .
WORKDIR "/src/."
# Build the project in Release configuration and output the application to /app/build
RUN dotnet build "myWebApi.csproj" -c Release -o /app/build
# Name the current stage of the build as 'publish'
FROM build AS publish
# Publish the applicationin Release configuration to a folder named 'publish'
RUN dotnet publish "myWebApi.csproj" -c Release -o /app/publish

# NAme the current stage of the build as final
FROM base AS final
# Change working directory in the container to '/app'
WORKDIR /app
# Copy the application from the 'publish' stage to the current stage
COPY --from=publish /app/publish .
# Set the command that will be run when Docker container is started
ENTRYPOINT ["dotnet", "myWebApi.dll"]

Make sure to replace myWebApi.csproj and myWebApi.dll with the name of your project if you are using a different name.

Build the Docker image

After creating the Dockerfile, you can build the Docker image by running the following command from your project directory:

docker build -t my-web-api .

This command tells Docker to build an image from the current directory (.) and name it my-web-api. Ensure that you run this command in the same directory where your Dockerfile is located.

Run the Docker container

Once the Docker image is built successfully, you can run a Docker container with the following command:

docker run -d -p 5011:80 --name my-web-api-container my-web-api

This command runs a container in detached mode (-d), maps port 5011 on your host to port 80 in the container, and names the container as my-web-api. Adjust the port numbers and container name as needed.

The my-web-api-container is the name of the container we are creating.

The my-web-api is the name of the image we built in the previous step.

Access the ASP.NET Web API

Your ASP.NET Web API is now running inside a Docker container. You can access it by opening a web browser or using a tool like curl, Postman or Insomnia and navigating to http://localhost:5011/your-controller (replace your-controller with the name of your Web API controller).

Note that you won't be able to use Swagger in the Docker container since the project was build with Release configuraiton.

If you want to use Swagger inside your Docker container you can simply move the lines in Program.cs file considering development environment condition check before creating your Docker image:

if (app.Environment.IsDevelopment())
{
  // Move these lines out of the if block
    app.UseSwagger();
    app.UseSwaggerUI();
}

Conculsion

Using Docker with ASP.NET Web API provides a consistent and efficient way to develop, test, and deploy your web services. By containerizing your ASP.NET Web API application, you ensure that it can run consistently across different environments, making deployment and scaling more manageable. This tutorial only scratches the surface of Docker's capabilities with .NET, but it should give you a good starting point to incorporate containers into your .NET Web API projects. Happy coding!

SHARE