Skip to main content

Docker & Kubernetes for .NET and Angular Developers

 In the fast-paced world of modern web development, containerization and orchestration technologies like Docker and Kubernetes have become essential tools for building, deploying, and managing applications. For developers working with .NET (especially .NET Core and later) on the backend and Angular on the frontend, understanding these technologies is more than just a nice-to-have—it's critical for efficient development and scalable deployment.

This blog is aimed at .NET and Angular developers who want to get up to speed with Docker and Kubernetes. We’ll cover the basics, dive into setting up a simple application using Docker, and explore how Kubernetes can help manage containers in a production-ready environment.


Why Docker and Kubernetes Matter

Before diving into the "how", let’s answer the "why".

The Old Way

Traditionally, deploying .NET applications (especially .NET Framework) was tightly coupled with Windows servers, IIS configurations, and heavyweight deployments. On the frontend, Angular apps were often served through static file hosting or bundled with backends, adding complexity to deployments.

Each environment—dev, staging, production—could behave differently. Dependencies might vary. Things would “work on my machine” but fail elsewhere.

Enter Docker

Docker allows developers to containerize their applications. A container is a lightweight, standalone, executable package that includes everything needed to run the app: code, runtime, libraries, and dependencies. Containers eliminate the "it works on my machine" problem.

For a .NET and Angular stack:

  • Docker can run your .NET API in a Linux or Windows container.

  • It can build and serve your Angular SPA inside an NGINX container.

  • Both can be run together using Docker Compose.

Enter Kubernetes

Kubernetes (K8s) takes containers a step further. It allows you to:

  • Deploy and manage multiple containers (microservices or monoliths) at scale.

  • Handle auto-scaling, self-healing, load balancing, and rolling updates.

  • Deploy in cloud-native environments or on-premises.

Kubernetes is a container orchestration platform—a powerful tool to manage containers in production.


Setting the Scene: A Sample Application

Let’s imagine a basic web app:

  • Backend: ASP.NET Core Web API that connects to a database.

  • Frontend: Angular app that consumes the API.

  • Database: PostgreSQL.

We want to:

  1. Containerize both frontend and backend.

  2. Run them locally using Docker Compose.

  3. Deploy the same setup to Kubernetes.


Step 1: Dockerizing the .NET Backend

Assume you have a .NET 7 Web API project called MyApp.API.

Dockerfile for .NET API:

Dockerfile
# Stage 1: Build FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build WORKDIR /app COPY *.csproj ./ RUN dotnet restore COPY . ./ RUN dotnet publish -c Release -o out # Stage 2: Runtime FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS runtime WORKDIR /app COPY --from=build /app/out ./ ENTRYPOINT ["dotnet", "MyApp.API.dll"]

Key Points:

  • Two-stage build: minimizes image size.

  • Uses official Microsoft base images.

  • Publishes app in Release mode.


Step 2: Dockerizing the Angular Frontend

Let’s say your Angular project is named myapp-client.

Dockerfile for Angular App:

Dockerfile
# Stage 1: Build the Angular app FROM node:18 AS build WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build --prod # Stage 2: Serve with NGINX FROM nginx:alpine COPY --from=build /app/dist/myapp-client /usr/share/nginx/html COPY nginx.conf /etc/nginx/nginx.conf

Optional NGINX Config (nginx.conf):

nginx
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; try_files $uri $uri/ /index.html; } }

Step 3: Docker Compose for Local Development

Create a docker-compose.yml file to run everything together.

yaml
version: '3.8' services: api: build: context: ./MyApp.API ports: - "5000:80" environment: - ASPNETCORE_ENVIRONMENT=Development client: build: context: ./myapp-client ports: - "4200:80" depends_on: - api db: image: postgres environment: POSTGRES_USER: myuser POSTGRES_PASSWORD: mypass POSTGRES_DB: mydb ports: - "5432:5432" volumes: - pgdata:/var/lib/postgresql/data volumes: pgdata:

Running It:

bash
docker-compose up --build

This setup brings up:


Step 4: Kubernetes — Going Beyond Local

Once your app works locally in Docker, you can move to Kubernetes. There are different ways to deploy:

  • Minikube (local cluster)

  • Docker Desktop Kubernetes

  • AKS (Azure Kubernetes Service)

  • EKS (AWS), GKE (Google)

Let’s focus on a basic Kubernetes deployment using manifests.


Kubernetes Deployment Files

You’ll need:

  1. Deployment and Service for each component.

  2. A ConfigMap or Secret for credentials (optional).

  3. A PersistentVolume for PostgreSQL.

API Deployment (api-deployment.yaml):

yaml
apiVersion: apps/v1 kind: Deployment metadata: name: api-deployment spec: replicas: 1 selector: matchLabels: app: myapp-api template: metadata: labels: app: myapp-api spec: containers: - name: api image: myregistry/myapp-api:latest ports: - containerPort: 80 env: - name: ASPNETCORE_ENVIRONMENT value: Production --- apiVersion: v1 kind: Service metadata: name: api-service spec: selector: app: myapp-api ports: - port: 80 targetPort: 80 type: ClusterIP

Angular Frontend (client-deployment.yaml):

yaml
apiVersion: apps/v1 kind: Deployment metadata: name: client-deployment spec: replicas: 1 selector: matchLabels: app: myapp-client template: metadata: labels: app: myapp-client spec: containers: - name: client image: myregistry/myapp-client:latest ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: client-service spec: selector: app: myapp-client ports: - port: 80 targetPort: 80 type: LoadBalancer

PostgreSQL (db-deployment.yaml):

yaml
apiVersion: apps/v1 kind: Deployment metadata: name: db-deployment spec: replicas: 1 selector: matchLabels: app: myapp-db template: metadata: labels: app: myapp-db spec: containers: - name: postgres image: postgres env: - name: POSTGRES_USER value: myuser - name: POSTGRES_PASSWORD value: mypass - name: POSTGRES_DB value: mydb ports: - containerPort: 5432 volumeMounts: - name: dbdata mountPath: /var/lib/postgresql/data volumes: - name: dbdata emptyDir: {} --- apiVersion: v1 kind: Service metadata: name: db-service spec: selector: app: myapp-db ports: - port: 5432 targetPort: 5432

Step 5: Deploying to Kubernetes

To deploy the app:

bash
kubectl apply -f db-deployment.yaml kubectl apply -f api-deployment.yaml kubectl apply -f client-deployment.yaml

Check pods and services:

bash
kubectl get pods kubectl get svc

Step 6: CI/CD Integration (Optional but Powerful)

You can automate Docker image builds and Kubernetes deployments using CI/CD pipelines with:

  • GitHub Actions

  • Azure DevOps

  • GitLab CI

Example GitHub Actions steps:

  • Build Docker image

  • Push to container registry

  • Deploy to Kubernetes cluster

Comments

Popular posts from this blog

Laravel 10 — Build News Portal and Magazine Website (2023)

The digital landscape is ever-evolving, and in 2023, Laravel 10 will emerge as a powerhouse for web development . This article delves into the process of creating a cutting-edge News Portal and Magazine Website using Laravel 10. Let’s embark on this journey, exploring the intricacies of Laravel and the nuances of building a website tailored for news consumption. I. Introduction A. Overview of Laravel 10 Laravel 10 , the latest iteration of the popular PHP framework, brings forth a myriad of features and improvements. From enhanced performance to advanced security measures, Laravel 10 provides developers with a robust platform for crafting dynamic and scalable websites. B. Significance of building a News Portal and Magazine Website in 2023 In an era where information is king, establishing an online presence for news and magazines is more crucial than ever. With the digital audience constantly seeking up-to-the-minute updates, a well-crafted News Portal and Magazine Website beco...

Laravel 10 — Build News Portal and Magazine Website (2023)

Learn how to create a stunning news portal and magazine website in 2023 with Laravel 10 . Follow this comprehensive guide for expert insights, step-by-step instructions, and creative tips. Introduction In the dynamic world of online media, a powerful content management system is the backbone of any successful news portal or magazine website. Laravel 10, the latest iteration of this exceptional PHP framework, offers a robust platform to build your digital empire. In this article, we will dive deep into the world of Laravel 10 , exploring how to create a news portal and magazine website that stands out in 2023. Laravel 10 — Build News Portal and Magazine Website (2023) News websites are constantly evolving, and Laravel 10 empowers you with the tools and features you need to stay ahead of the game. Let’s embark on this journey and uncover the secrets of building a successful news portal and magazine website in the digital age. Understanding Laravel 10 Laravel 10 , the most recent vers...

Google Ads MasterClass 2024 - All Campaign Builds & Features

  Introduction to Google Ads in 2024 Google Ads has evolved tremendously over the years, and 2024 is no different. Whether you are a small business owner, a marketer, or someone looking to grow their online presence, Google Ads is an essential tool in today’s digital landscape. What Is Google Ads? Google Ads is a powerful online advertising platform that allows businesses to reach potential customers through search engines, websites, and even YouTube. It gives businesses the ability to advertise their products or services precisely where their audience is spending their time. From local businesses to global enterprises, Google Ads helps companies of all sizes maximize their online visibility. The Importance of Google Ads for Modern Businesses In 2024, online competition is fiercer than ever. Businesses need to stand out, and Google Ads offers a way to do that. With the platform's variety of ad formats and targeting options, you can reach people actively searching for your product ...