What it is
A container is a group of ordinary processes running on a host's kernel, fenced off so that they see their own file system, process list, network interfaces and hostname, and limited in how much CPU, memory and I/O they may use. It is started from a container image: a packaged, read-only file system with the app, its runtime and its libraries, plus metadata (entry command, environment, exposed ports). Images and runtimes follow the OCI (Open Container Initiative) specifications, so images built with one tool run with another (Docker, containerd, CRI-O, Podman).
Why it exists
"It works on my machine" usually means the machine differs: library versions, runtime versions, OS packages. An image freezes all of that, so the same bytes run on a laptop, in CI and in production. Because a container is just processes, it starts in milliseconds to seconds and costs little memory beyond the app itself, so one host can run dozens of them. That speed and density make frequent deploys, fast scaling and fast recovery practical.
How it works
Images and layers. An image is built from a recipe (a Dockerfile or equivalent). Each instruction that changes files produces a read-only layer; the layers are stacked with a union file system (overlayfs) and the container gets a thin writable layer on top. A layer is reused from the build cache only if it and everything below it are unchanged, so put rarely changing things (base OS, runtime, dependencies) at the bottom and the app code last. Registries store layers by content hash; a host pulls only the layers it lacks.
Digests and tags. Every image has a digest, a SHA-256 over its manifest, which identifies exactly one set of contents. Tags (web:2.0, latest) are movable names. Deploying by digest makes deploys reproducible and rollbacks exact.
Namespaces. Linux namespaces give a process group its own view of a kernel resource: PID (own process tree), network (own interfaces, routes, ports), mount (own file system tree), UTS (hostname), IPC, user (UID mapping, so root inside can be unprivileged outside) and cgroup.
cgroups. Control groups account for and limit resources: CPU (shares and quotas; a container over its CPU quota is throttled), memory (a container over its limit is OOM-killed by the kernel), block I/O and process counts.
Runtime. A high-level runtime (containerd, CRI-O) pulls images, prepares the file system and networking, and asks a low-level runtime (runc, crun) to create the namespaces and cgroups and start the process. A restart policy can start a container again when it exits.
Immutable deploys. You do not log in and change a running container. You build a new image, start containers from it, move traffic to them and stop the old ones. Configuration comes from the environment, mounted files or a secrets store, so the same image runs everywhere.
When to use it
- Stateless services that you deploy often and scale horizontally.
- Many small services on shared hosts, where a VM per service would waste memory.
- Reproducible builds and environments across development, CI and production.
- Batch jobs and workers that start, run and exit.
When not to
- Untrusted or multi-tenant code on a shared kernel, unless you add a sandboxed runtime (gVisor, Kata Containers, microVMs).
- Software that needs its own kernel, kernel modules or a different OS.
- Large stateful systems where operating them in containers adds risk without benefit; a managed service or dedicated VMs may be simpler.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Containers instead of VMs | Fast starts, density, reproducible images | Shared kernel, weaker isolation, limits to tune |
| Small base images (distroless, Alpine) | Smaller pulls, fewer CVEs | Harder debugging; musl vs glibc differences with Alpine |
| Tight memory limits | Protects neighbours, predictable packing | OOM kills when the limit is too low |
| CPU limits | Caps noisy containers | Throttling and latency spikes even when the host is idle |
| Rolling replace | No extra fleet, no downtime | Old and new versions run side by side for a while |
Alternatives
- VMs: stronger isolation and full OS control, slower boots and more overhead.
- MicroVMs (Firecracker) and sandboxed runtimes: VM-grade isolation with container-like starts.
- Serverless functions: no images or hosts to manage for short, event-driven work (many platforms now accept container images too).
- Language-level packaging (a single static binary, a JAR): simpler, but the OS and runtime are still the host's.
Failure modes
- OOM kills: a container exceeding its memory limit is killed, often repeatedly (a restart loop).
- CPU throttling: a low CPU limit stretches latency even on an idle host.
- Image drift: mutable tags such as
latestmake two environments run different code. - Deploy error bursts: stopping containers before traffic has moved away from them.
- Huge images: slow pulls slow every deploy and scale-out; a bad layer order invalidates the cache on every build.
- Kernel-level escape: a kernel or runtime vulnerability crosses the container boundary.
- Lost data: anything written to the container's writable layer disappears with the container; use volumes or external storage.
In production
Build small images with a sensible layer order and scan them for vulnerabilities. Pin base images and deploy by digest. Run as a non-root user with minimal capabilities, a seccomp profile and a read-only root file system where possible. Set memory limits from measurements, be careful with CPU limits, and expose readiness and liveness endpoints. Deploy with start → ready → drain → stop, and keep previous images in the registry for rollbacks. On more than a handful of hosts, let an orchestrator place, restart and replace containers.
Common misconceptions
- "A container is a small VM." It is processes on the host kernel; there is no guest kernel.
- "Containers are secure by default." The boundary is real but thinner than a VM's; hardening and sandboxing matter for untrusted code.
- "The image is the container." The image is the read-only template; containers are running instances with their own writable layer.
- "Restarting a container fixes it." It restores a clean state from the image; if the bug or the limit is still there, it fails again.
Interview angle
Explain containers versus VMs in one sentence (shared kernel versus own kernel), then the consequences: start-up time, density, isolation. Know layers and caching well enough to fix a slow build, and why digests beat tags. For deployments, describe a zero-downtime rollout (new instance first, readiness gate, drain, stop) and how rollback works with immutable images. Mention resource limits and what happens when they are hit (throttling, OOM kills).
Model assumptions
This lesson uses a simplified model, not a benchmark. Each container is a simulated server with 2 workers and a 25 ms mean service time (lognormal, cv 0.35), about 80 req/s; traffic is Poisson. The image build, layer push and pull, and container start messages are a scripted illustration, with illustrative durations, not a simulated registry. OOM kills, restarts, starts and stops are timed actions on a compressed timeline. Draining is the balancer giving a container weight 0. Health checks run every 250 ms; 2 failures eject, 2 successes admit. Utilisation is the fraction of busy workers.