22. Container Internals
Docker is often described as “magic,” but it is actually just a sophisticated wrapper around several pre-existing Linux kernel features. You do not need to be a kernel developer to use Docker, but understanding the underlying mechanics of how containers are isolated and executed will make you a significantly better DevOps engineer.
22.1 Linux Namespaces
Section titled “22.1 Linux Namespaces”If cgroups govern how much a container can use, Namespaces govern what a container can see. Namespaces are the mechanism the Linux kernel uses to isolate system resources. Docker combines several namespaces to create the illusion of a completely isolated operating system.
- PID (Process ID) Namespace: Isolates the process tree. This is why the main application inside a container thinks it is running as
PID 1, and why it cannot see or interact with any processes running on the host machine or in other containers. - NET (Network) Namespace: Isolates the network stack. It provides the container with its own private IP address, routing tables, firewall rules, and virtual ethernet interfaces.
- MNT (Mount) Namespace: Isolates the filesystem mount points. The container believes its root directory (
/) is the entire hard drive, completely unaware of the host’s actual filesystem. - UTS (UNIX Time-Sharing) Namespace: Isolates the hostname and domain name. This allows the container to have a different hostname than the host machine.
- IPC (Inter-Process Communication) Namespace: Prevents processes in one container from accessing the shared memory segments of processes in another container.
- USER Namespace: Allows a process to have root privileges (UID 0) inside the container, while actually mapping to an unprivileged, restricted user on the physical host machine (drastically improving security).
22.2 cgroups (Control Groups)
Section titled “22.2 cgroups (Control Groups)”While Namespaces provide isolation, cgroups provide resource management. Developed originally by Google, cgroups prevent a single process from monopolizing the host’s hardware.
- CPU: Limits the amount of CPU cycles the container can consume, either as a hard quota or a relative priority weight.
- Memory: Limits the total RAM the container can allocate. If the limit is breached, the cgroup triggers the OOM (Out Of Memory) Killer to terminate the process.
- Process Limits: Restricts the total number of child processes a container can spawn, preventing “Fork Bomb” denial-of-service attacks.
22.3 The Container Runtime Architecture
Section titled “22.3 The Container Runtime Architecture”Docker is not a single monolithic block of code; it is a modular stack. When you type docker run, the command cascades down through several discrete components before hitting the Linux Kernel.
Docker CLI (The terminal command) ↓ Docker Daemon (`dockerd` - REST API & Image Management) ↓ containerd (High-level Container Supervisor) ↓ runc (Low-level Container Runtime) ↓ Linux Kernel (Namespaces & cgroups)- Docker Daemon (
dockerd): The background service that manages high-level tasks like building images from Dockerfiles, pushing/pulling from registries, and managing networks/volumes. - containerd: An industry-standard core container runtime. It manages the lifecycle of the container (starting, pausing, stopping) and handles the pulling of image layers. (It was originally part of Docker but was spun out as an independent CNCF project).
- runc: The ultra-low-level tool that actually interfaces directly with the Linux Kernel. Its only job is to create the namespaces and cgroups, spawn the container process, and immediately exit.
(Note: Because of this modularity, modern Kubernetes dropped the Docker Daemon entirely and now communicates directly with containerd to run containers!)
22.4 Union Filesystems & Storage
Section titled “22.4 Union Filesystems & Storage”How can you spin up 50 Ubuntu containers instantly without consuming 50 times the disk space? The secret is Union Filesystems (specifically, the overlay2 storage driver).
Image Layers
Section titled “Image Layers”A Docker image is composed of multiple read-only layers. Each instruction in a Dockerfile (FROM, RUN, COPY) creates a new layer on top of the previous one.
Copy-on-Write (CoW)
Section titled “Copy-on-Write (CoW)”Union filesystems stack these read-only layers on top of each other, presenting them to the container as a single, unified filesystem. If a container needs to modify a file that exists in a lower, read-only image layer, it does not modify the original layer. Instead, it copies the file up to its top writable layer and modifies it there. This is called Copy-on-Write.
The Writable Layer
Section titled “The Writable Layer”When a container starts, a thin, ephemeral read-write layer is placed on top of the read-only image stack. Because all underlying image layers are read-only and shared across every container using that image, starting 50 identical containers consumes virtually zero extra disk space!