How to Containerize .NET Core Web API Using Kubernetes And Docker

In this post, I’ll teach you how to develop the.Net Core API application step by step, enable Docker in it, and then run it in Kubernetes after producing images.



  1. Basic Understanding of Cloud.
  2. Basic Understanding of Docker.


1. Create a new ASP.NET Core Web API Project


2. Configure the new Project


3.Set extra project information such as Target Framework, Authentication Type, Enable Https, Enable Docker, Docker OS Support, and so on.


Include the “DemoController” new controller. When we call the get method, it will return Welcome to the Docker World.


using Microsoft.AspNetCore.Mvc;
using System;
namespace WebAPI.Controllers {
    public class DemoController: ControllerBase {
        public String Get() {
            return "Welcome to the Docker World!";


When we enable the Docker Support option while creating a new Web Application, Visual Studio automatically generates the Dockerfile shown below.


#See to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM AS base
FROM AS build
COPY ["WebAPI/WebAPI.csproj", "WebAPI/"]
RUN dotnet restore "WebAPI/WebAPI.csproj"
COPY . .
RUN dotnet build "WebAPI.csproj" -c Release -o /app/build

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

FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebAPI.dll"]


The Docker Image will then be created in Docker Desktop, which you installed on your local computer, when the program is then executed using Docker. (Please verify that the Docker Desktop is installed successfully and is in running mode on your computer.)


Here you will see the output after executing the get method of DemoController.






  • For managing their own microservices across many clusters, Google built the container management orchestration tool known as Kubernetes.
  • The Numeronym Standard, commonly known as Kubernetes or K8s, has been in use since the 1980s. For instance, there are 8 words between the letters K and S in the K8s.
  • The Borg and Omega internal systems that Google developed are used to orchestrate the data center.
  • Google released Kubernetes as an open source project in 2014; it was created in the Golang programming language. Donated later to CNCF.
  • A program called Kubernetes automates the deployment of containers, load balancing, and auto-scaling. All of the containers running in various settings will be managed by it.

Now that we’re running this application on Kubernetes, let’s enable Kubernetes in Docker Desktop as demonstrated below.


We build a manifest file afterwards. In essence, it is used for configuration and might be of the YML and JSON types because we discussed settings pertaining to various objects and how they will be connected to one another. The Manifest File contains a variety of items that are necessary to execute the application, including information on the number of pods, services, deployment, and ingress controller.

apiVersion: apps/v1
kind: Deployment
  name: webapi
    app: product-app
  replicas: 2
      service: webapi
        app: product-app
        service: webapi
        - name: webapicontainer
          image: webapi:dev
            - containerPort: 80
              protocol: TCP
            - name: ASPNETCORE_URLS
              value: http://+:80
apiVersion: v1
kind: Service
  name: webapiservice
    app: product-app
    service: webapi
  type: NodePort
    - port: 8080
      targetPort : 80
      protocol: TCP
    service: webapi


Therefore, in this portion of the manifest file, we mentioned the deployment and service sections and established the settings for pods, services, ports, and Docker images and containers.

Next, we are going to apply Manifest YAML File using kubectl Kubernetes


kubectl apply -f manifest.yml



Here you will see our service is running on Port 30360 and now we are able to access that using URL



Use the following commands to get more information about pods, services, endpoints, deployments, and the Kubernetes environment.


You can use one of the various commands available that are used to scale containers in accordance with the needs of your project.

This was all about utilizing Docker and Kubernetes to containerize the.NET Core Web API.

I trust you comprehended the Docker-related topics.


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories