DEV Community

Cover image for How to Set Up Docker and Kubernetes for Web Apps: A Beginner's Guide
Raji moshood
Raji moshood

Posted on

How to Set Up Docker and Kubernetes for Web Apps: A Beginner's Guide

Introduction

As modern web applications grow in complexity, managing deployments efficiently becomes a challenge. Docker and Kubernetes simplify this by enabling containerization and orchestration. This guide will introduce both technologies, walk through their setup, and show how they work together for scalable web apps.

What is Docker?

docker
Docker is a containerization platform that allows developers to package applications and their dependencies into isolated environments called containers.

Why Use Docker?

Consistency – Works the same in development and production.

Lightweight – Containers use fewer resources than virtual machines.

Easy deployment – Package everything into one image and deploy anywhere.

Scalability – Works well with cloud services and orchestration tools like Kubernetes.

Installing Docker

For Windows & Mac

  1. Download and install Docker Desktop.

  2. Open a terminal and verify installation:

docker --version
Enter fullscreen mode Exit fullscreen mode

For Linux (Ubuntu/Debian)

  1. Update package lists:
sudo apt update && sudo apt install -y docker.io
Enter fullscreen mode Exit fullscreen mode
  1. Start Docker:
sudo systemctl start docker
Enter fullscreen mode Exit fullscreen mode
  1. Enable Docker to run on startup:
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode
  1. Verify installation:
docker --version
Enter fullscreen mode Exit fullscreen mode

Building and Running a Docker Container

  1. Create a simple Node.js app
mkdir myapp && cd myapp
Enter fullscreen mode Exit fullscreen mode

Create an index.js file:

const http = require('http');
const server = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello, Docker!');
});
server.listen(3000, () => console.log('Server running on port 3000'));
Enter fullscreen mode Exit fullscreen mode
  1. Create a Dockerfile (defines how to build your container)
FROM node:18  
WORKDIR /app  
COPY . .  
RUN npm install  
CMD ["node", "index.js"]  
EXPOSE 3000
Enter fullscreen mode Exit fullscreen mode
  1. Build and run the Docker container
docker build -t myapp .
docker run -p 3000:3000 myapp
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:3000 in your browser.

What is Kubernetes?

kubernetes
Kubernetes (K8s) is an orchestration platform that automates the deployment, scaling, and management of containerized applications.

Why Use Kubernetes?

Automated Scaling – Handles increasing workloads.

Self-Healing – Restarts failed containers automatically.

Load Balancing – Distributes traffic efficiently.

Multi-Cloud Support – Runs on AWS, GCP, Azure, and on-premise.

Installing Kubernetes (Minikube for Local Testing)

  1. Install kubectl (Kubernetes CLI):
sudo apt install -y kubectl
Enter fullscreen mode Exit fullscreen mode
  1. Install Minikube (Lightweight Kubernetes for local use):
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
Enter fullscreen mode Exit fullscreen mode
  1. Start Minikube:
minikube start
Enter fullscreen mode Exit fullscreen mode
  1. Verify Kubernetes installation:
kubectl version --client
Enter fullscreen mode Exit fullscreen mode

Deploying a Dockerized App to Kubernetes

  1. Create a Kubernetes Deployment YAML file (deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: myapp:latest
          ports:
            - containerPort: 3000
Enter fullscreen mode Exit fullscreen mode
  1. Apply the deployment
kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode
  1. Expose the application via a service
kubectl expose deployment myapp --type=NodePort --port=3000
Enter fullscreen mode Exit fullscreen mode
  1. Get the service URL and access the app
minikube service myapp --url
Enter fullscreen mode Exit fullscreen mode

Docker vs. Kubernetes: What's the Difference?

docker vs kubernetes
Docker is for containerization, while Kubernetes is for container orchestration.

Docker requires manual scaling, but Kubernetes automatically scales applications.

Docker does not include built-in load balancing, whereas Kubernetes has load balancing out of the box.

Docker does not have self-healing capabilities, but Kubernetes can automatically restart failed containers.

Docker is suitable for single applications, while Kubernetes is ideal for distributed applications.

When to Use Docker and Kubernetes

✔ Use Docker if you need to containerize your application for easy deployment.
✔ Use Kubernetes if you need to manage, scale, and automate multiple containers.

Conclusion

Docker and Kubernetes are powerful tools for modern web app development. Docker makes it easy to containerize applications, while Kubernetes helps manage them at scale.

By following this guide, you now have a basic understanding of both tools and can start building scalable, containerized web apps.

I am open to collaboration on projects and work. Let's transform ideas into digital reality.

Docker #Kubernetes #DevOps #WebDevelopment #CloudComputing #Containers

Top comments (0)