Chapter 36 — Containers & the 12-Factor App
Part VII · Delivery | Prerequisites: Ch 3, Ch 8 | Builds: Pulsely containerized with a production multi-stage Dockerfile + Compose
Earlier you saw what a process actually is — a thread of execution the kernel schedules, given its own virtual address space, its own file descriptors, and a fence around what it can touch. You met the kernel mechanisms (namespaces, cgroups) almost in passing, as facts about how the OS keeps tenants apart. This chapter is where that knowledge pays off. A container is not a new kind of thing you have to learn from scratch; it is a process you already understand, wearing a costume the kernel sews for it. Once you see that, the whole edifice — images, layers, registries, Compose, even Kubernetes — stops being magic and becomes mechanics.
But packaging a process is only half the job. A box that boots reliably is worthless if the application inside it can't survive being run five times at once, killed without warning, and moved between machines. So this chapter has two halves that lock together: containers (the packaging) and the twelve-factor discipline (the application constraints that make the package replicable). One without the other is a trap. We'll derive both from problems, then containerize Pulsely for real.
1. The problem: "works on my machine," and three deploys that hurt
Here is a scene every backend engineer eventually lives through. Pulsely renders patient-facing report PDFs and DICOM thumbnails, and that path uses sharp, a native module that compiles against system libraries. On the author's laptop it works perfectly — Node 20, sharp globally cached, a .env file nobody committed (holding the database URL and a live PHI-store credential) sitting helpfully in the project root. CI is green-ish until the image step, where it dies: the runner has a different glibc, sharp's prebuilt binary doesn't match, and npm ci recompiles against headers that aren't there. Staging dies differently — same code, different distro, a missing environment variable that lived only in the author's shell. Debugging becomes archaeology across three environments, none of which is the same as the other two. The bug isn't in the code. The bug is that the code was never the unit being shipped — the whole environment was, implicitly, and nobody controlled it.
Now widen the lens to deployment. The classic small-team deploy is: SSH to the box, git pull, npm ci, pm2 restart. It works on day one. By day ninety, that box is a snowflake — a one-off no other machine matches. Someone hotfixed a config by hand at 2 a.m. and never wrote it down. A migration ran half-way and stopped. node_modules has a leftover from a dependency you removed three sprints ago. No two servers are byte-identical, so a bug that reproduces on server #2 and not #4 has no explanation you can find. And "rollback" means git checkout the previous SHA and rebuild on the box — hoping it still builds, during an outage, while clinicians stare at 500s mid-visit.
And scale. To run five copies of the API, the snowflake model says: provision five VMs. Each is a two-minute boot plus a config-management run, each carries a full guest OS to run your 200 MB process, and you're bin-packing 200 MB of work into 4 GB of metal. Wasteful in money, slow in time, fragile in operation.
The promise this chapter cashes: ship the exact filesystem and process the developer tested, as one immutable artifact, that boots in roughly a tenth of a second and packs many-to-a-host. No "works on my machine," because your machine — the relevant parts of it — is what ships. But that promise has a condition attached, and the condition is the second half of the chapter: the artifact is only safe to replicate if the app inside obeys rules that make "just run another copy" actually correct. Containers buy you the package; twelve-factor buys you the right to run it many times.
2. The mental model: a process the kernel lied to, frozen into a tarball
Hold four pictures.
A container is a process, not a machine. This is the single most important sentence in the chapter. When you "run a container," the kernel starts an ordinary Linux process and then lies to it: it tells the process that it has its own root filesystem (a private /), that it is PID 1, that it has its own network interface, and that the machine has only the CPU and RAM you allotted. There is no guest operating system, no hypervisor, no second kernel. The container shares the host's kernel and is scheduled by it exactly like any other process. Strip the lies away and a container is node dist/server.js — just one that can't see your files, can't see your other processes, and can't use more than its slice.
🧭 Mental model. A VM virtualizes hardware and runs a whole guest OS on top; a container virtualizes the view a process has of the OS and runs no guest at all. "VM = a fake computer; container = a process wearing blinders." This is why containers boot in milliseconds (it's just
fork/execwith extra setup) and VMs boot in tens of seconds (a kernel has to come up). It is also why the isolation is weaker — a topic for §8.
An image is a tarball, not a running thing. An image is a frozen filesystem plus a little metadata (what command to run, what ports it declares, what env it defaults). It is the class; a container is the instance. "Run image → container" is "construct an object from a class." You can have one image and a hundred running containers off it, the way one class spawns a hundred objects. Nothing in an image is executing; it's inert bytes until something instantiates it.
Build time and run time are different worlds, and the line between them is sacred. The image is frozen at build. Everything that varies between environments — the database URL, the Redis host, the secret keys, which region you're in — arrives at run time, injected from outside. This single boundary is the spine of the whole chapter. Most of twelve-factor is, at heart, rules enforcing exactly this separation: bake the code, inject the config.
A twelve-factor process is a near-pure function of (request, env, backing services). Give it the same request, the same environment variables, and the same database, and any copy produces the same answer. It holds nothing important in its own memory between requests. That property — and only that property — is what makes "kill one, start another, run five" safe. The container is the packaging; this statelessness is what makes the packaging worth having. And it sets up the final picture: an orchestrator is a control loop to which you declare "I want N healthy copies of image X reachable at one address," and which continuously reconciles reality toward that wish. Hold that; we open it in §4.
3. Theory & invariants
A mental model tells you how to picture containers. Invariants tell you what stays true no matter which tool builds them.
🔑 The isolation invariant: same kernel, two knobs. A container is a process placed in fresh namespaces (what it can see) with cgroup limits (what it can use), running on a private root filesystem. Namespaces are the costume — the three you can actually feel are PID (its own process tree, so it thinks it's the only process, PID 1), mount (its own filesystem view), and network (its own interfaces and ports). A few more round it out — its own hostname (UTS), its own shared-memory channels between processes (IPC), and its own user-ID mapping — same idea, different resource. Cgroups are the leash — CPU shares, a hard memory cap, a pids limit, I/O throttling. Isolation is namespaces; resource control is cgroups. That's the entire trick. Everything Docker does is orchestration around these two kernel features you've already met.
A corollary you must internalize: the kernel is shared, always. A container cannot run a different kernel than its host. This is why "Linux containers on a Mac or Windows" silently spins up a hidden Linux VM — the Linux container needs a Linux kernel, and your laptop doesn't have one to share. It also bounds the security guarantee: container isolation is process-level, enforced by the host kernel, not VM-level enforced by a hypervisor. We pay for that in §8.