Docker Isn't Magic: Building a Container by Hand with Raw Linux
Most engineers use Docker every day without ever seeing what it’s actually doing underneath. I wanted to close that gap for myself — so I rebuilt a container from scratch, using nothing but raw Linux tools: no Docker, no containerd, no Podman.
The goal
Section titled “The goal”A container isn’t a special virtualization technology. It’s a normal Linux process with restrictions layered on top — restrictions that come from features already built into the kernel: namespaces, cgroups, chroot, and OverlayFS. I wanted to prove that by building one by hand and seeing exactly where each piece of “container magic” actually comes from.
What I built
Section titled “What I built”Working from an Alpine Linux minimal root filesystem, I:
- Constructed a layered filesystem with OverlayFS — a read-only base image layer, a writable layer to capture changes, and a merged view presented to the container as its root filesystem. This is the same mechanism that keeps a Docker image immutable while still letting a running container write files.
- Isolated the process with Linux namespaces, using
unshareto strip a shell process of what it would normally share with the host — its view of other processes, its hostname, its network stack, and its mount points. Once isolated, that shell became PID 1 inside its own sandboxed world. - Locked the process to its new root filesystem with
chroot, so it could only see the Alpine filesystem I’d built — not the host’s. - Applied a memory limit with cgroups, confirming that resource limits work identically whether a process is “in a container” or not — it’s the same kernel feature either way.
- Wired up networking manually, creating a virtual Ethernet pair to bridge the container’s isolated (and initially empty) network namespace back to the host, including NAT so the container could reach the internet.
- Installed and ran nginx inside the sandbox, verified it was reachable from both inside the container and from the host, then inspected the writable layer to confirm the base image had stayed completely untouched.
What went wrong (and what that taught me)
Section titled “What went wrong (and what that taught me)”Building this by hand surfaced problems Docker normally hides completely:
- A missing
PATHinside the chrooted shell, becausechrootchanges the filesystem root but not the environment it inherited from the host. - A loopback interface that existed but wasn’t actually up — because unsharing the network namespace doesn’t hand you a working network, it hands you an empty one. Even
localhosttraffic doesn’t work until you explicitly bringloup yourself.
Neither of these is a bug in Linux — they’re exactly the kind of setup work container runtimes do silently on your behalf, every single time you run docker run.
Closing the loop: making it Docker-compatible
Section titled “Closing the loop: making it Docker-compatible”Once the sandbox was working, I took it one step further: turned the same filesystem into an actual Docker image using docker import, then rebuilt the same result again properly as a Dockerfile. Lining the two up side by side made the connection unmistakable — every docker build instruction maps directly onto one of the manual steps above. FROM alpine is the minirootfs extraction. Each RUN apk add is the same package install done by hand inside the chroot. CMD is the same nginx command typed manually into the sandboxed shell. docker run is unshare + chroot + veth networking, wrapped up into one command.
Docker isn’t doing anything fundamentally different from what’s above — it’s just doing it faster, with far better error handling, and without making you find out the hard way that a fresh network namespace doesn’t even have a working loopback interface.
Why this matters
Section titled “Why this matters”Understanding containers at this level changes how you reason about them in production. It explains why containers have near-zero overhead compared to virtual machines — they’re just constrained processes, not a separate virtualization layer. It explains why a base image layer never changes no matter how many containers run from it. And it makes Kubernetes primitives like ConfigMaps, StatefulSets, and DaemonSets click differently, because they’re higher-level abstractions sitting on top of exactly these same kernel features.
This kind of “under the hood” understanding is what I look for whenever I pick up a new area of platform engineering — not just knowing the commands, but knowing what they’re actually doing to the system, so troubleshooting in production isn’t guesswork.
Full step-by-step technical walkthrough, including the exact commands, cgroup setup, and network configuration, is available in the project repo.