Nine spreads. Each has a picture, the idea, and the commands and YAML you'd actually use, from your first container to a running cluster.
"We package the app as an immutable image, and Kubernetes keeps the desired number of replicas running, healthy, and reachable."
Use the tabs or the ← → keys to turn pages.
A container is an ordinary process with walls around it. An image is the recipe and files; a container is a running instance of an image, like a program versus a process.
$ docker version $ docker run hello-world $ docker run -d -p 8080:80 --name web nginx # host 8080 → container 80 $ curl localhost:8080 $ docker ps ; docker ps -a $ docker logs -f web $ docker exec -it web sh # shell inside it $ docker stop web ; docker rm web
"Containers are isolated processes sharing the host kernel, not tiny VMs."
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
HEALTHCHECK CMD node -e "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1))"
CMD ["node", "server.js"]node_modules .git .env *.log
$ docker build -t myapp:1.0 . $ docker images ; docker history myapp:1.0 $ docker run --rm -p 3000:3000 --env-file .env myapp:1.0 $ docker tag myapp:1.0 ghcr.io/you/myapp:1.0 ; docker push ghcr.io/you/myapp:1.0
"Small base image, non-root user, deps cached before code, and no secrets in layers."
$ docker volume create pgdata $ docker network create appnet $ docker run -d --name db --network appnet \ -e POSTGRES_PASSWORD=devpass -v pgdata:/var/lib/postgresql/data postgres:16 $ docker run -d --name api --network appnet -p 3000:3000 \ -e DATABASE_URL=postgres://postgres:devpass@db:5432/postgres myapp:1.0 $ docker run -v "$PWD":/app -p 3000:3000 myapp:dev # bind mount for live edits $ docker volume ls ; docker network ls ; docker inspect api
db resolves to its containerdocker system prune removes stopped containers and dangling images"State lives in volumes, services find each other by name, and only the edge is published."
services:
web:
build: ./web
ports: ["8080:80"]
depends_on: [api]
api:
build: ./api
environment:
DATABASE_URL: postgres://postgres:${DB_PASSWORD}@db:5432/app
depends_on:
db: { condition: service_healthy }
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes: [pgdata:/var/lib/postgresql/data]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
volumes:
pgdata:$ docker compose up -d --build ; docker compose ps $ docker compose logs -f api $ docker compose down # add -v to also delete volumes
"Compose for local dev and CI, with secrets in a .env file that never gets committed."
Docker runs containers on one machine. Kubernetes (K8s) runs them across many: scheduling, restarting, scaling, and routing traffic. You describe the desired state in YAML, and it reconciles reality to match.
kubectl, goes through it$ kind create cluster # or: minikube start $ kubectl cluster-info $ kubectl get nodes $ kubectl get pods -A # -A = all namespaces $ kubectl config get-contexts # which cluster am I pointing at?
"It's a control loop: observe actual state, compare with desired state, act."
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels: { app: myapp }
template:
metadata:
labels: { app: myapp }
spec:
containers:
- name: myapp
image: ghcr.io/you/myapp:1.0
ports: [{ containerPort: 3000 }]
resources:
requests: { cpu: 100m, memory: 128Mi }
limits: { memory: 256Mi }
readinessProbe:
httpGet: { path: /health, port: 3000 }
livenessProbe:
httpGet: { path: /health, port: 3000 }$ kubectl apply -f deployment.yaml $ kubectl get deploy,pods -l app=myapp $ kubectl scale deploy myapp --replicas=5 $ kubectl set image deploy/myapp myapp=ghcr.io/you/myapp:1.1 $ kubectl rollout status deploy/myapp ; kubectl rollout undo deploy/myapp
"Three replicas, resource requests, readiness and liveness probes, and a rolling update with one-command rollback."
apiVersion: v1
kind: Service
metadata: { name: myapp }
spec:
selector: { app: myapp } # matches pod labels
ports: [{ port: 80, targetPort: 3000 }]
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: { name: myapp }
spec:
ingressClassName: nginx
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service: { name: myapp, port: { number: 80 } }$ kubectl get svc,ingress $ kubectl port-forward svc/myapp 8080:80 # test locally $ kubectl run tmp --rm -it --image=busybox -- wget -qO- myapp
myapp.default.svc.cluster.local, or just myapp in the same namespace"Ingress routes by host and path to ClusterIP Services, which select pods by label."
$ kubectl create configmap app-config --from-literal=LOG_LEVEL=info $ kubectl create secret generic db-secret --from-literal=password='s3cr3t' $ kubectl get secret db-secret -o yaml # base64 ≠ encryption
containers:
- name: myapp
image: ghcr.io/you/myapp:1.0
envFrom:
- configMapRef: { name: app-config }
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef: { name: db-secret, key: password }
volumeMounts:
- { name: data, mountPath: /var/lib/data }
volumes:
- name: data
persistentVolumeClaim: { claimName: app-data }dev, staging, prodkubectl autoscale deploy myapp --min=3 --max=10 --cpu-percent=70"Config in ConfigMaps, secrets from a vault, persistent data on PVCs, and an HPA for load."
$ kubectl get pods -o wide $ kubectl describe pod myapp-7d9f-abc12 # read the Events at the bottom $ kubectl logs myapp-7d9f-abc12 $ kubectl logs myapp-7d9f-abc12 --previous # the crashed container $ kubectl logs -l app=myapp -f --tail=50 $ kubectl exec -it myapp-7d9f-abc12 -- sh $ kubectl get events --sort-by=.lastTimestamp $ kubectl top pods # needs metrics-server $ kubectl get endpoints myapp # empty? labels don't match $ kubectl delete pod myapp-7d9f-abc12 # Deployment recreates it
"Describe the pod, check previous logs, verify the Service has endpoints, and compare against the same image under plain Docker."