Introduction: Containers and Beyond Docker and Kubernetes are often mentioned together, leading to confusion about their roles. Docker is a container runtime and image format. Kubernetes is a container orchestration platform. Understanding this distinction is fundamental for anyone building modern infrastructure.
This guide provides a comprehensive comparison, explaining what each technology does, when to use which, and how they complement each other.
What is Docker? Docker revolutionized software deployment by popularizing containerization. A container packages an application with all its dependencies into a standardized, portable unit.
Core Components
Component
Purpose
Docker Engine
The runtime that creates and manages containers
Docker Image
Template containing application and dependencies
Dockerfile
Instructions for building images
Docker Compose
Multi-container application definition
Docker Hub
Public registry for sharing images
Docker Architecture 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ┌─────────────────────────────────────────────┐ │ Docker Desktop / CLI │ └─────────────────────────────────────────────┘ │ ┌─────────────────────────────────────────────┐ │ Docker Daemon (dockerd) │ │ ┌─────────────┐ ┌──────────────────────┐ │ │ │ Container │ │ Container │ │ │ │ (App + │ │ (App + │ │ │ │ Deps) │ │ Deps) │ │ │ └─────────────┘ └──────────────────────┘ │ │ ┌───────────────────────────────────────┐ │ │ │ Container Runtime (containerd) │ │ │ └───────────────────────────────────────┘ │ │ ┌───────────────────────────────────────┐ │ │ │ Linux Kernel (cgroups, namespaces)│ │ │ └───────────────────────────────────────┘ │ └─────────────────────────────────────────────┘
Installing Docker on Ubuntu/Debian 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 sudo apt remove docker docker-engine docker.io sudo apt update sudo apt install -y ca-certificates curl gnupg lsb-release sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/debian/gpg | \ sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \ https://download.docker.com/linux/debian $(lsb_release -cs) stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt update sudo apt install -y docker-ce docker-ce-cli containerd.io sudo docker run hello-world
Basic Docker Commands 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 docker run -d --name nginx -p 80:80 nginx:latest docker ps docker logs nginx docker exec -it nginx bash docker build -t myapp:1.0 . docker push myregistry/myapp:1.0
Docker Compose Example Create docker-compose.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 version: '3.8' services: web: image: nginx:alpine ports: - "80:80" volumes: - ./html:/usr/share/nginx/html:ro restart: unless-stopped db: image: postgres:15 environment: POSTGRES_USER: app POSTGRES_PASSWORD: secret POSTGRES_DB: myapp volumes: - db_data:/var/lib/postgresql/data restart: unless-stopped volumes: db_data:
Deploy:
1 2 3 docker-compose up -d docker-compose ps docker-compose logs -f
What is Kubernetes? Kubernetes (K8s) automates deployment, scaling, and management of containerized applications across clusters of machines.
Core Concepts
Concept
Description
Pod
Smallest deployable unit (one or more containers)
Deployment
Manages pod replicas and updates
Service
Network abstraction for accessing pods
Namespace
Logical cluster partition
Ingress
HTTP/HTTPS routing rules
ConfigMap/Secret
Configuration and sensitive data
PersistentVolume
Storage abstraction
Kubernetes Architecture 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ┌─────────────────────────────────────────────────────┐ │ Control Plane │ ├─────────────────────────────────────────────────────┤ │ ┌─────────────────┐ ┌──────────────────────────┐ │ │ │ API Server │ │ etcd (State Store) │ │ │ │ (kube-apiserver)│ │ │ │ │ └─────────────────┘ └──────────────────────────┘ │ │ ┌─────────────────┐ ┌──────────────────────────┐ │ │ │ Scheduler │ │ Controller Manager │ │ │ │ (kube-scheduler)│ │ (kube-controller) │ │ │ └─────────────────┘ └──────────────────────────┘ │ └─────────────────────────────────────────────────────┘ │ ┌─────────────────────────────────────────────────────┐ │ Worker Nodes │ ├─────────────────────────────────────────────────────┤ │ ┌───────────────────────────────────────────────┐ │ │ │ kubelet (Agent) │ │ │ │ ┌─────────────┐ ┌───────────────────────┐ │ │ │ │ │ Pod │ │ Pod │ │ │ │ │ │ ┌─────────┐ │ │ ┌─────────────────┐ │ │ │ │ │ │ │Container│ │ │ │ Container │ │ │ │ │ │ │ └─────────┘ │ │ └─────────────────┘ │ │ │ │ │ │ ┌─────────┐ │ │ ┌─────────────────┐ │ │ │ │ │ │ │Container│ │ │ │ Container │ │ │ │ │ │ │ └─────────┘ │ │ └─────────────────┘ │ │ │ │ │ └─────────────┘ └───────────────────────┘ │ │ │ └───────────────────────────────────────────────┘ │ │ ┌───────────────────────────────────────────────┐ │ │ │ kube-proxy (Networking) │ │ │ └───────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────┘
Installing Kubernetes (k3s lightweight distribution) Single Node Setup:
1 2 3 4 5 6 7 8 9 10 11 12 curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --disable traefik" sh - mkdir -p ~/.kubesudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config sudo chown $(id -u):$(id -g) ~/.kube/config export KUBECONFIG=~/.kube/configkubectl get nodes kubectl get pods --all-namespaces
Add Worker Node:
1 2 curl -sfL https://get.k3s.io | K3S_URL=https://master-ip:6443 \ K3S_TOKEN=<token> sh -
Basic Kubernetes Commands 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 kubectl create deployment nginx --image=nginx:latest kubectl scale deployment nginx --replicas=3 kubectl expose deployment nginx --port=80 --type =LoadBalancer kubectl get nodes kubectl get pods -o wide kubectl get services kubectl get deployments kubectl describe pod <pod-name> kubectl logs <pod-name> kubectl exec -it <pod-name> -- /bin/bash kubectl apply -f deployment.yaml kubectl apply -f service.yaml
Kubernetes Deployment Example Create deployment.yaml:
```yaml apiVersion: apps/v1 kind: Deployment metadata: name: webapp labels: app: webapp spec: replicas: 3 selector: matchLabels: app: webapp template: metadata: labels: app: webapp spec: containers: - name: