๐Ÿณ Docker & Kubernetes Fundamentals

How containers package applications, and how Kubernetes orchestrates them at scale โ€” explained with diagrams from the container up to the cluster.

1. Containers vs Virtual Machines

A container packages an application with everything it needs to run โ€” code, runtime, libraries, config โ€” but shares the host machine's OS kernel. A virtual machine virtualizes an entire hardware stack, including its own OS. This makes containers dramatically lighter and faster to start.

Virtual Machines Infrastructure Host OS Hypervisor Guest OSBins/LibsApp A Guest OSBins/LibsApp B Containers Infrastructure Host OS Container Engine Bins/LibsApp A Bins/LibsApp B

Figure 1 โ€” VMs virtualize hardware and duplicate the OS per app; containers share one OS kernel via a container engine.

2. Docker Core Concepts

๐Ÿ“„ Dockerfile

A text file with instructions to build an image โ€” base OS, dependencies, code, and startup command.

๐Ÿ–ผ๏ธ Image

An immutable, layered snapshot built from a Dockerfile. Stored in a registry (Docker Hub, ECR, GCR).

๐Ÿ“ฆ Container

A running instance of an image โ€” an isolated process with its own filesystem, network, and process space.

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

Each instruction (FROM, COPY, RUN) creates a cached image layer โ€” unchanged layers are reused on rebuild, which is why Dockerfiles are usually ordered from least- to most-frequently-changing steps.

3. From One Container to a Cluster

Running a single container is easy. Running hundreds of containers reliably โ€” restarting failed ones, load-balancing traffic, scaling under demand โ€” is where Kubernetes comes in. Kubernetes is a container orchestration platform that automates deployment, scaling, and management of containerized applications.

4. Kubernetes Architecture

Control Plane API Serverentry point Schedulerplaces pods Controller Managerdesired state loop etcdcluster state store Worker Nodes Node 1 Pod A Pod B kubelet + kube-proxy Node 2 Pod C Pod D kubelet + kube-proxy Node 3 Pod E Pod F kubelet + kube-proxy

Figure 2 โ€” The Control Plane decides "what should run where"; worker nodes actually run the pods via kubelet.

ComponentRole
API ServerFront door for all cluster operations (kubectl talks to this)
SchedulerDecides which node a new pod should run on
Controller ManagerContinuously reconciles actual state โ†’ desired state
etcdDistributed key-value store holding all cluster state
kubeletAgent on each node that starts/stops containers as instructed
kube-proxyHandles networking rules so pods can talk to each other

5. Key Kubernetes Objects

Pod

The smallest deployable unit โ€” one or more tightly-coupled containers sharing storage/network. Pods are ephemeral; they get replaced, not repaired.

Deployment

Declares the desired number of pod replicas and manages rolling updates. If a pod dies, the Deployment controller creates a new one automatically.

Service

A stable network endpoint (IP + DNS name) that load-balances traffic across a set of pods, even as individual pods come and go.

ConfigMap / Secret

Externalized configuration and sensitive values, injected into pods without baking them into the container image.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels: { app: web }
  template:
    metadata:
      labels: { app: web }
    spec:
      containers:
        - name: web
          image: myregistry/web-app:1.4.0
          ports: [{ containerPort: 3000 }]
---
apiVersion: v1
kind: Service
metadata:
  name: web-app-service
spec:
  selector: { app: web }
  ports: [{ port: 80, targetPort: 3000 }]
  type: LoadBalancer

This YAML tells Kubernetes: "always keep 3 replicas of this container running, and expose them behind a single load-balanced address."

6. Self-Healing in Action

Pod (healthy) Podโœ• crashed Controllerdetects drift from desired state New Podscheduled automatically

Figure 3 โ€” When a pod dies, the Controller Manager notices the mismatch and schedules a replacement without human intervention.

7. Quick Reference

CommandWhat it does
docker build -t app:1.0 .Build an image from a Dockerfile
docker run -p 3000:3000 app:1.0Run a container, mapping host:container ports
kubectl apply -f deployment.yamlCreate/update resources from a YAML manifest
kubectl get podsList running pods in the current namespace
kubectl scale deployment web-app --replicas=5Scale a deployment up or down
kubectl logs <pod-name>View container logs for debugging