Module 6 · Reliability and infrastructureLesson 69 of 887 minBeginner
Docker and containers
A container packages an app with everything it needs, and runs it isolated on a shared operating system kernel. It's lighter and faster than a virtual machine.
Fill the server with VMs and note how many fit and how long each takes to start. Clear it and fill it with containers.
"It works on my machine" used to be a daily problem: different library versions, different OS settings, a missing dependency. Containers fix it by shipping the app with its environment.
Images and containers
- A Dockerfile describes how to build the app: start from a base (like
node:22-slim), copy the code, install dependencies, set the start command. - Building it produces an image: a read-only bundle of layers.
- Running an image creates a container: an isolated process with its own filesystem, network and limits.
- Images are stored in a registry (Docker Hub, GitHub Container Registry, ECR) and pulled wherever they need to run.
Containers vs virtual machines
| Virtual machine | Container | |
|---|---|---|
| Includes | Full guest OS + app | App + its libraries |
| Isolation | Strong (hypervisor) | Good (kernel features) |
| Start time | Seconds to minutes | Milliseconds to seconds |
| Size | Gigabytes | Megabytes |
Under the hood, Linux namespaces give each container its own view of processes, files and network, and cgroups limit its CPU and memory.
Why it matters for system design
- Consistency: the exact image tested in CI runs in production.
- Density: many more services per machine.
- Speed: fast starts make autoscaling and rolling deploys practical.
- Immutable deploys: don't patch servers; build a new image and replace the containers.
Good habits
- Keep images small (slim base images, multi-stage builds).
- One main process per container.
- Don't store data inside containers; they're disposable. Use volumes or external databases.
- Never bake secrets into images; inject them at runtime.
Running many containers across many machines needs an orchestrator. That's Kubernetes, next.