Containerize .NET 8.0 Web API with Docker

6 minute read

1. Docker

Docker is an open-source platform, and it is used to containerize the application. It packages the application along with the dependencies into the containers, which is easier to deploy.

Read more about Docker and its CLI on Docker CLI

2. Setup Web API project in .NET 8

2.1 Create a new ASP.NET Core Web API project

  1. Open Visual Studio 2022.
  2. Go to File -> New -> Project.
  3. Select “ASP.NET Core Web API”

Fig 1: Create ASP.NET Core Web API

2.2 Configure your new project

  1. Enter project name
  2. Enter Location
  3. Provide solution details

Fig 2: Configure your new project

2.3 Additional Information

  1. Select framework - .NET 8.0 (Long Term Support).
  2. Choose your authentication type - Default ‘None’.
  3. Uncheck HTTPS to disable local SSL certificate
  4. Uncheck ‘Enable container support’ to set up our own Docker file for practice purposes.
  5. Select ‘Use controllers’

Fig 3: Additional Information

2.4 Default controller - WeatherForecast

  1. The default controller called ‘WeatherForecast’ will be created and placed inside the controllers folder named as ‘WeatherForecastController.cs’
  2. Open the file to see the default methods to get the weather forecast details.

Fig 4: Default controller - Weather forecast

2.5 Run ‘Get’ API of Weather forecast

  1. Press ‘F5’ to build and run the Web API project.
  2. Opens the URL - http://localhost:portno/weatherforecast and returns the result in JSON format.

Fig 5: Weather forecast - GET API

3. Containerize Web API: Docker Setup, Build, and Run

3.1. Create Docker file

  1. Right-click the Web API project -> Add -> Choose ‘Docker support’.

    Fig 6: Docker support
  2. Opens the ‘Container Scaffolding Options’.

    Fig 7: Container Scaffolding Options

3.2. Dockerfile content

A new Docker file will be created. Replace the content with the following code:

  1. The ‘FROM’ keyword creates a new build stage from a base image.
  2. ‘MCR’ stands for Microsoft Container Registry (mcr.microsoft.com).
  3. The ‘EXPOSE’ instruction allows you to expose the port to access the application.
  4. The ‘ENTRYPOINT’ instruction sets ‘dotnet’ as host for the ‘DockerSample.API.dll’.

Code:

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

  RUN dotnet restore "./DockerSample.API.csproj" --disable-parallel
  RUN dotnet publish "./DockerSample.API.csproj" -c release -o /app --no-restore

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

  # Expose the port your application will run on
  EXPOSE 5000

  # Start the application
  ENTRYPOINT ["dotnet","DockerSample.API.dll"]

Screenshot:


Fig 8: Docker file content

3.3. Docker Build

  1. Open the terminal (Powershell) and navigate the Web API project path.
  2. Run the Docker build command to build the image.
  3. Syntax: Docker build –rm -t [dockerimagename]:latest .
  4. Example: docker build –rm -t dockerwebapiimage:latest .

-t stands for tag to tag the image.
–rm option automatically removes the container when it exits.

Note:

The above steps 3 & 5 end with ‘.’, which is required to set the current directory.


Fig 9: Docker Build

3.4. Docker Run

A Docker container is a running instance of a Docker image. It allows you to package the application code and its dependencies into a single unit.

  1. Run the below command to create a docker container from a docker image.
  2. Syntax: docker run -d -p host:portno:defaultport [imagename]
  3. Example: docker run -d -p 127.0.0.1:5000:8080 dockerwebapiimage
  4. Returns the container ID as a response.

-p stands for port number.

-d indicates the container should run in detached mode.

Note: The port number ‘5000’ that was exposed in the docker file has been mapped to the default port.


Fig 10: Docker Run - create container

3.5. Docker Desktop

Docker Desktop can be used in Windows and Mac to manage the various Docker components, including containers, images, volumes, etc.

  1. Go to Docker Desktop.

    Fig 11: Docker Desktop
  2. Containers

    List all the Docker containers. You can see the newly created container in the list and identify the same with the container ID.


    Fig 12: Docker Containers
  3. Container Details

    Shows the details of the selected container. The logs show the default port number, and the same has been mapped to the Docker file exposed port number during the Docker run execution.


    Fig 13: Docker Container details

3.6. Output (Localhost)

Open the browser and enter the following url: http://localhost:5000


Fig 14: Docker Run - Localhost

Leave a comment