Docker vs Kubernetes - Understanding Container Fundamentals and Orchestration

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
# Remove old versions
sudo apt remove docker docker-engine docker.io

# Install dependencies
sudo apt update
sudo apt install -y ca-certificates curl gnupg lsb-release

# Add Docker GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | \
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Setup repository
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

# Install Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

# Test installation
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
# Run a container
docker run -d --name nginx -p 80:80 nginx:latest

# List running containers
docker ps

# View logs
docker logs nginx

# Execute command in container
docker exec -it nginx bash

# Build an image
docker build -t myapp:1.0 .

# Push to registry
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
# Master node
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --disable traefik" sh -

# Configure kubectl
mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $(id -u):$(id -g) ~/.kube/config
export KUBECONFIG=~/.kube/config

# Verify
kubectl 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
# Create deployment
kubectl create deployment nginx --image=nginx:latest
kubectl scale deployment nginx --replicas=3

# Expose service
kubectl expose deployment nginx --port=80 --type=LoadBalancer

# View resources
kubectl get nodes
kubectl get pods -o wide
kubectl get services
kubectl get deployments

# Describe resources
kubectl describe pod <pod-name>

# Logs and exec
kubectl logs <pod-name>
kubectl exec -it <pod-name> -- /bin/bash

# Apply YAML configuration
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: