Why this lab exists
Every time you run docker run, something happens that most developers take for granted: the process inside the container cannot see the host’s processes, cannot write to the host’s filesystem, and cannot consume unlimited memory. These are not Docker features. They are Linux kernel features, and Docker is just a user-space orchestrator that calls them.
This lab removes the abstraction. You build a container runtime from scratch in three stages, calling the same Linux syscalls that runc — the OCI reference runtime used by Docker and Kubernetes — calls. By the end you will know:
- Why a containerized process sees PID 1 (and what that really means)
- Why
pivot_rootexists and whychrootis not enough for containers - How OverlayFS makes it possible to share image layers across hundreds of containers without copying any data
- Why cgroup v2 is structurally different from cgroup v1 and why Kubernetes requires it
- What Testcontainers does under the hood when you write
@Container static PostgreSQLContainer<?> postgres
The mental shift this lab produces: containers are not virtual machines. They are processes with extra syscall arguments.
What we’re building
A container runtime in Go, built across three progressive stages. Each stage adds one isolation primitive.
We build:
- Linux namespace isolation: UTS (hostname), PID (process tree), mount (
/proc) viaCLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS - cgroup v2 resource limits:
memory.max,cpu.weight,pids.maxwritten to/sys/fs/cgroup/ - OverlayFS root filesystem: image layers as read-only lower dirs, per-container upper dir, merged view via
pivot_root - A CLI entry point:
container-runtime run alpine /bin/sh
We explicitly skip (and why):
- Network namespace (
CLONE_NEWNET) and veth pairs — networking is a full lab by itself - User namespace (
CLONE_NEWUSER) — UID mapping requires/etc/subuidand complicates the examples - OCI image registry and layer decompression — containerd handles this; runc receives an already-unpacked bundle
- Container lifecycle daemon (pause, resume, kill, delete) — the state machine is real but obscures the core isolation lessons
Linux-only note: all isolation primitives in this lab are Linux kernel features. The code carries //go:build linux tags. On macOS or Windows the binary exits with a descriptive message. To experiment on non-Linux, run a Linux VM or use docker run --rm -it --privileged ubuntu:22.04 /bin/bash.
Mental model
A container is a process whose kernel view has been restricted at three levels:
Level 1 — Namespaces (what the process can SEE)
UTS namespace: process has its own hostname
PID namespace: process sees its own process tree; it is PID 1
mount namespace: process has its own mount table
Level 2 — cgroups (what the process can CONSUME)
memory.max: kernel OOM-kills if memory exceeds limit
cpu.weight: kernel scheduler gives proportional CPU time
pids.max: fork() fails if process count exceeds limit
Level 3 — Root filesystem (what the process can ACCESS)
OverlayFS: lower dirs = read-only image layers
upper dir = per-container write layer
merged dir = unified view (what the container sees)
pivot_root: makes merged dir the container's /
(host root is unmounted; container cannot escape)
The traditional confusion is between these three. Namespaces control visibility. cgroups control resource consumption. The root filesystem controls file access. A container needs all three; most “container escapes” exploit a gap in one of them.
The re-exec pattern is how Go container runtimes initialize the child after clone. Because Go’s runtime starts multiple threads before main(), you cannot safely call clone() from a goroutine and then do namespace setup (signal handlers, /proc mounts, sethostname) in the child. The solution: the binary re-execs itself (/proc/self/exe) as the child process, with an environment variable (CONTAINER_INIT=1) signalling the child to run initialization code before exec-ing the real command.
Prerequisites
- Linux kernel 5.10 or later (cgroup v2 unified hierarchy)
- Go 1.23 or later
- Root privileges or
CAP_SYS_ADMIN + CAP_NET_ADMIN(namespace creation requires elevated permissions) - A basic understanding of how
fork()andexec()work — the lab builds on that mental model
No prior container knowledge is assumed. We build up from first principles.
Stage-by-stage build
Stage 0 Linux Namespace Isolation
- Fork a child process into UTS, PID, and mount namespaces via SysProcAttr.Cloneflags
- Set the container hostname via syscall.Sethostname in the child
- Mount /proc in the child's mount namespace so the PID namespace is visible
- Verify: child sees PID 1; host process table is unchanged
- Test: namespace flags set correctly, container ID generates correctly, state string is sane
The core insight of v0: containers start with clone(), not fork(). Linux’s clone() syscall is like fork() but accepts flags that specify which kernel resources the child should share with the parent or get fresh copies of.
Go exposes this via syscall.SysProcAttr.Cloneflags:
// namespace.go — the three flags that make a minimal container
func namespaceAttrs() *syscall.SysProcAttr {
return &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | // new hostname/domain namespace
syscall.CLONE_NEWPID | // new process tree (child = PID 1)
syscall.CLONE_NEWNS, // new mount table
}
}CLONE_NEWUTS (Unix Time Sharing namespace) gives the child its own hostname. Without this, syscall.Sethostname would change the host’s hostname — a global side effect. With it, the child’s hostname is sandboxed.
CLONE_NEWPID is the “PID 1” flag. The first process the kernel starts in the new namespace appears to itself as PID 1. The host can still see the process (it has a host PID), but from inside the namespace the process tree starts at 1. This is why ps aux inside a Docker container shows a short list — not because Docker hides processes, but because the kernel only shows the container’s PID namespace.
CLONE_NEWNS (mount namespace) gives the child a copy of the parent’s mount table. Critically, this is a copy — any mounts the child makes (like /proc) are invisible to the parent. Without this flag, mounting /proc inside the container would affect the host’s /proc mount.
The re-exec pattern solves Go’s goroutine scheduler problem. After cmd.Start() returns, the child process is running /proc/self/exe init (the same binary) with CONTAINER_INIT=1 set. The child’s main() detects this env var and calls containerInit():
// container.go — child-side initialization
func containerInit() error {
id := os.Getenv("CONTAINER_ID")
// UTS namespace: set the container's hostname
if err := syscall.Sethostname([]byte(id[:12])); err != nil {
return fmt.Errorf("sethostname: %w", err)
}
// Mount namespace: mount /proc so the PID namespace is visible
// Without this, tools like ps, top, kill won't work inside the container.
syscall.Mount("proc", "/proc", "proc", 0, "")
// Parse real command and exec — replaces this init process
parts := strings.Split(os.Getenv("CONTAINER_CMD"), "\x00")
binary, _ := exec.LookPath(parts[0])
return syscall.Exec(binary, parts, os.Environ())
}The final syscall.Exec is critical. It replaces the init process with the real command. The container process is now PID 1 in its namespace and is running the requested command — just like Docker’s entrypoint.
What the tests check: the namespaceAttrs() function returns Cloneflags with all three bits set. NewContainer generates an ID with the ctr- prefix and a nanosecond timestamp. ContainerState() includes the ID and PID in its output.
Stage 1 cgroups v2 Resource Limits
- Create a cgroup hierarchy under /sys/fs/cgroup/container-runtime/<id>/
- Write memory.max, cpu.weight, pids.max before the container process starts
- Add the container PID to cgroup.procs to move it into the cgroup
- Read current usage from memory.current and cpu.stat
- Cleanup: remove the cgroup directory after the container exits
- Test: files are written with correct values, cleanup removes the directory
cgroups (control groups) are the kernel mechanism for limiting what a process can consume. cgroup v2, the unified hierarchy, uses a single tree under /sys/fs/cgroup/. Every resource controller is a file in the cgroup directory.
Creating a container’s cgroup is four file operations:
// cgroup.go — Apply() sequence
const cgroupRoot = "/sys/fs/cgroup/container-runtime"
func (m *CgroupManager) Apply(cfg *CgroupConfig, pid int) error {
// 1. Create the cgroup directory
os.MkdirAll(m.cgroupPath, 0755)
// 2. Write resource limits
if cfg.MemoryLimitBytes > 0 {
m.writeFile("memory.max", strconv.FormatInt(cfg.MemoryLimitBytes, 10))
}
if cfg.CPUShares > 0 {
m.writeFile("cpu.weight", strconv.FormatInt(cfg.CPUShares, 10))
}
if cfg.MaxPIDs > 0 {
m.writeFile("pids.max", strconv.Itoa(cfg.MaxPIDs))
}
// 3. Move the process into the cgroup — this is the critical step
m.writeFile("cgroup.procs", strconv.Itoa(pid))
return nil
}The files are sysfs pseudo-files — writing to them is a syscall that updates kernel data structures. The kernel enforces limits from that point forward. If memory usage exceeds memory.max, the kernel OOM-kills a process in the cgroup. If the process count hits pids.max, any fork() or clone() inside the cgroup returns EAGAIN.
cgroup v1 vs v2 difference: in cgroup v1, each controller had its own tree (/sys/fs/cgroup/memory/, /sys/fs/cgroup/cpu/, etc.). A process had to be added to each tree separately. cgroup v2 uses a single unified hierarchy — one directory, multiple controller files. Kubernetes 1.25+ requires cgroup v2 for this reason: the unified hierarchy makes accounting and coordination much simpler.
Reading resource usage is also just file reads:
// memory.current — current RSS + page cache in bytes
func (m *CgroupManager) Usage() (*ResourceUsage, error) {
memBytes, _ := m.readFile("memory.current")
// cpu.stat contains "usage_usec <N>" — total CPU time in microseconds
cpuStat, _ := m.readFile("cpu.stat")
// ... parse usage_usec field
}Cleanup requires the cgroup to be empty first. The kernel refuses to remove a cgroup directory with processes in it. After the container process exits, cgroup.procs is empty and os.Remove(m.cgroupPath) succeeds.
The timing constraint: cgroup setup must happen after the child process is started (so you have its PID) but before it execs the real command (otherwise the limits apply after the command has already started consuming resources). The re-exec pattern gives us this window — the child spins waiting in containerInit() while the parent writes the cgroup files.
What the tests check: CgroupPath() returns the correct path under /sys/fs/cgroup/container-runtime/. Tests that require root skip with t.Skip() if not running as root. When running as root: memory.max contains the expected value, cpu.weight is set, pids.max is set, the current PID is in cgroup.procs. After Cleanup(), the directory no longer exists.
Stage 2 OverlayFS + pivot_root
- Mount an OverlayFS with image layers as lowerdir and a per-container upperdir
- Call pivot_root to make the merged directory the container's root filesystem
- Unmount the old root and remove the .put_old directory
- Verify: writes inside the container appear in upperdir, not in image layers
- Test: buildLowerDir reverses layer order correctly, overlay mount creates writable merged view
The filesystem isolation problem: if two containers share the same image, you cannot give each one a copy of the image (that would be slow and wasteful). OverlayFS solves this by separating reads from writes.
The three-layer model:
lowerdir: /images/ubuntu/layer1:/images/ubuntu/layer0 (read-only, shared across containers)
upperdir: /containers/ctr-123/upper (read-write, per-container)
workdir: /containers/ctr-123/work (OverlayFS internal bookkeeping)
merged: /containers/ctr-123/merged (what the container sees as /)When the container reads a file, OverlayFS checks upperdir first, then lowerdir layers from left to right. When the container writes a file, the write goes to upperdir only — the image layers are untouched. This is copy-on-write at the filesystem level.
Mounting OverlayFS is one syscall with a specially formatted options string:
// overlay.go
opts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s",
buildLowerDir(cfg.ImageLayers), // newest layer leftmost (highest priority)
cfg.UpperDir,
cfg.WorkDir)
syscall.Mount("overlay", cfg.MergedDir, "overlay", 0, opts)Layer order matters. In the lowerdir string, the leftmost path has highest priority. If a file exists in both layer1 and layer0, the version in layer1 wins. Our buildLowerDir reverses the ImageLayers slice (which stores oldest-first) to produce the correct lowerdir string.
pivot_root is the correct way to change a process’s root filesystem. chroot is not enough for containers because a process with CAP_SYS_CHROOT can escape a chroot by opening a directory before chrooting and using it as a reference to the real root. pivot_root moves the entire mount namespace root, which is much harder to escape.
The sequence is unavoidably fiddly because the kernel requires specific pre-conditions:
// overlay.go — pivot_root sequence
func PivotRoot(newRoot string) error {
// Step 1: newRoot must be a mount point — bind-mount it onto itself
syscall.Mount(newRoot, newRoot, "", syscall.MS_BIND|syscall.MS_REC, "")
// Step 2: create a directory inside newRoot to hold the old root
putOldDir := filepath.Join(newRoot, ".put_old")
os.MkdirAll(putOldDir, 0700)
// Step 3: swap newRoot with / — old / moves to /.put_old
syscall.PivotRoot(newRoot, putOldDir)
// Step 4: chdir is required after pivot_root
syscall.Chdir("/")
// Step 5: unmount the old root — container can no longer access host files
syscall.Unmount("/.put_old", syscall.MNT_DETACH)
// Step 6: clean up the mountpoint directory
os.Remove("/.put_old")
return nil
}After pivot_root, the container process’s / is the merged OverlayFS directory. The host root filesystem is gone from the container’s view. An ls / inside the container shows the image’s root directory, not the host’s. This is the last piece of isolation.
The full v2 container lifecycle:
MountOverlay(cfg)— set up OverlayFS on the hostcmd.Start()withnamespaceAttrs()— fork child into namespacescgroupMgr.Apply(cfg, pid)— write resource limits, move PID into cgroup- Child:
Sethostname,PivotRoot(mergedDir),Mount /proc,Exec(realCommand) - Parent:
cmd.Wait()— block until container exits cgroupMgr.Cleanup()+UmountOverlay(cfg)— remove cgroup dir, unmount overlay
What the tests check: buildLowerDir reverses the layer slice correctly. MountOverlay with real temp directories and root privileges succeeds and makes the merged dir writable. Writing to merged/ creates a file in upper/ (not in any lower layer).
Run it yourself
labs/container-runtime ↗
Linux kernel 5.10+Go 1.23+Root or CAP_SYS_ADMIN - 1 Clone and build (Linux only)
git clone https://github.com/pushkarkumar-dev/system-design-labs cd system-design-labs/labs/container-runtime go build ./... - 2 Run the tests (non-root tests run everywhere; cgroup/overlay tests need root)
go test ./... # To run all tests including cgroup/overlay: sudo go test ./... - 3 Start a container (requires root)
sudo go run ./cmd/run run ubuntu:22.04 /bin/sh - 4 Verify namespace isolation — inside the container
# Inside the container shell: hostname # shows container ID, not host hostname ps aux # shows only container processes ls /proc # container's /proc mount, not host's - 5 Build the Java demo (no Docker required for unit tests)
cd java-integration mvn test
--- PASS: TestNamespaceAttrs (0.00s)
--- PASS: TestNewContainer (0.00s)
--- PASS: TestContainerState (0.00s)
--- PASS: TestCgroupManagerPath (0.00s)
--- PASS: TestBuildLowerDir (0.00s)
PASS
ok github.com/pushkarkumar-dev/system-design-labs/labs/container-runtime/pkg/runtime 0.003s Benchmarks
Linux x86-64, 16 cores, 32GB RAM (container runtime requires Linux) | Metric | Result | Notes |
|---|---|---|
| Container start (namespace + cgroup, no OverlayFS) | 8 ms | estimated — fork + namespace setup, no image pull |
| Container start (full OverlayFS + pivot_root) | 45 ms | estimated — OverlayFS mount adds ~37ms |
| cgroup write latency (memory.max) | 0.2 ms | estimated — sysfs write, kernel cgroup setup |
| pivot_root syscall | 0.5 ms | estimated — bind mount + pivot + umount sequence |
Stress tests
What happens when you hit pids.max? Set MaxPIDs: 2 and run a command that tries to fork. The fork() inside the container returns EAGAIN — the system call fails with “resource temporarily unavailable.” The container process sees this as an error. The kernel does not kill anything; it simply refuses to create more processes. A container trying to run a shell script that forks a pipeline of commands will start failing at fork() once the limit is reached.
Memory limit enforcement is more abrupt. When a container’s memory usage approaches memory.max, the kernel first tries to reclaim page cache. If that is not enough, the OOM killer activates inside the cgroup — it kills the highest-scoring process in the cgroup. The process receives SIGKILL; the kernel does not send SIGTERM first. There is no graceful shutdown. This is why well-behaved containerized services set their JVM heap or allocator limits below the cgroup limit — they handle the out-of-memory condition themselves rather than relying on OOM kill.
OverlayFS under write pressure reveals the copy-on-write cost. The first write to a file in the lower layer requires the kernel to copy the entire file into the upper layer before modifying it. This is the “copy-up” operation. For a container that modifies a large file in the image (say, a database updating its system tablespace), copy-up can add significant latency to the first write. After copy-up, subsequent writes go directly to the upper layer at full speed.
Namespace cleanup on container crash: if the container process exits abnormally (SIGKILL, panic, OOM), the parent’s cmd.Wait() still returns and cleanup runs. Namespaces are reference-counted by the kernel — when the last process in a namespace exits, the kernel automatically reclaims the namespace. The cgroup directory must still be cleaned up explicitly (os.Remove(cgroupPath)), but it is always safe to do so after all processes have exited.
What the toy misses
- OCI image spec and layer decompression Docker images are OCI tarballs with a manifest.json and layer tarballs (gzip). Pulling an image requires fetching from a registry with auth, decompressing layers, and computing content digests. runc doesn't pull — containerd does. Our lab assumes pre-extracted layer directories.
- Network namespace and veth pairs Containers need their own network namespace (CLONE_NEWNET). Giving a container internet access requires creating a veth pair, moving one end into the container namespace, configuring IP addresses, and setting up NAT rules with iptables. This is a full lab by itself.
- User namespace and UID mapping Running containers as root is a security problem. User namespaces (CLONE_NEWUSER) map container root (UID 0) to an unprivileged host UID. This requires /etc/subuid and /etc/subgid entries and complicates the re-exec flow significantly.
- Seccomp system call filtering Docker's default seccomp profile blocks ~44 dangerous syscalls (e.g., keyctl, ptrace, mount from inside). Implementing seccomp requires loading a BPF filter via prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, ...). This is a significant security boundary that our runtime lacks entirely.
- Container lifecycle management runc handles exec, pause, resume, kill, delete. Each state transition requires updating state on disk and handling signal delivery. A full OCI runtime state machine has 7 states. Our runtime supports only start and wait.
- SELinux and AppArmor profiles Mandatory access control adds a policy layer beyond DAC. Setting the SELinux context for a container process requires the setexeccon() call and a policy that allows the container's expected syscall patterns. Without this, a compromised container can make syscalls the policy should forbid.
Wiring it into a Java/Spring service
Wiring it into a Java/Spring service
org.testcontainers:testcontainers:1.19.8 org.testcontainers:postgresql:1.19.8 org.springframework.boot:spring-boot-starter-test:3.3.0 labs/container-runtime/java-integration Clone and run: cd labs/container-runtime/java-integration && mvn spring-boot:run What Testcontainers does under the hood
Testcontainers is the primary way Java services get container isolation in integration tests. When you annotate a test with @Testcontainers and declare a @Container, Testcontainers calls the Docker daemon, which calls containerd, which calls runc. And runc does exactly what this lab does: namespaces, cgroups, OverlayFS, pivot_root.
A typical Spring Boot integration test using Testcontainers:
@SpringBootTest
@Testcontainers
class PostgresIntegrationTest {
@Container
static PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:16")
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");
@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}
@Autowired
private UserRepository userRepository;
@Test
void canSaveAndRetrieveUser() {
userRepository.save(new User("alice@example.com"));
assertThat(userRepository.findByEmail("alice@example.com")).isPresent();
}
}The delegation chain
What happens when PostgreSQLContainer.start() is called:
@Testcontainers / PostgreSQLContainer
│ calls Docker Engine REST API (UNIX socket /var/run/docker.sock)
▼
Docker daemon (dockerd)
│ delegates image management + container lifecycle to
▼
containerd
│ invokes via OCI runtime interface (shim)
▼
runc (reference OCI runtime — written in Go)
│ calls the Linux kernel:
│ clone(CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWNET | CLONE_NEWUSER)
│ write memory.max, cpu.weight, pids.max to /sys/fs/cgroup/
│ mount overlay -o lowerdir=...,upperdir=...,workdir=...
│ pivot_root(mergedDir, putOld)
│ exec postgres as PID 1
▼
Container process: postgres server, isolated hostname, isolated network,
isolated root filesystem, resource-capped cgroupOur Go lab implements the runc layer directly. Testcontainers adds the Docker API and image pull steps on top. The kernel operations are identical.
Part A — ContainerClient
ContainerClient documents the isolation stack and the delegation chain:
public class ContainerClient {
public String describeIsolation() {
return "Namespace isolation: UTS + PID + mount (+ NET + USER in Docker)\n"
+ "cgroup v2 limits: memory.max, cpu.weight, pids.max\n"
+ "Root filesystem: OverlayFS (lower=image layers, upper=container writes)\n"
+ "pivot_root: container process sees isolated / (not host /)\n"
+ "OCI runtime: runc (same syscalls as our Go lab, production-hardened)";
}
}Part B — ContainerProperties
ContainerProperties binds the container-lab prefix from application.yml:
container-lab:
image: postgres:16
database: labdb
startup-timeout-seconds: 60This is the Spring Boot typed-configuration pattern — no @Value annotations scattered across beans.
Part C — The pom.xml dependencies
Three coordinates give you full Testcontainers + JUnit 5 integration:
<!-- Testcontainers core — manages Docker lifecycle from tests -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.8</version>
<scope>test</scope>
</dependency>
<!-- Typed PostgreSQL container — knows the port, env vars, readiness probe -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.19.8</version>
<scope>test</scope>
</dependency>
<!-- JUnit 5 extension — @Testcontainers + @Container lifecycle -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.8</version>
<scope>test</scope>
</dependency>Part D — Unit tests (no Docker required)
The unit tests in ContainerClientTest verify the isolation description and configuration logic without requiring a running Docker daemon:
@Test
void describeIsolation_containsExpectedPrimitives() {
ContainerClient client = new ContainerClient(new ContainerProperties());
String description = client.describeIsolation();
assertThat(description).contains("UTS");
assertThat(description).contains("OverlayFS");
assertThat(description).contains("pivot_root");
assertThat(description).contains("cgroup");
}Run it
# Unit tests only (no Docker required)
cd labs/container-runtime/java-integration
mvn test
# Full demo output
mvn spring-boot:runExpected output:
=== Container Runtime Lab — Java/Testcontainers Perspective ===
Configured image: postgres:16
Isolation stack:
Image: postgres:16
Namespace isolation: UTS + PID + mount (+ NET + USER in Docker)
cgroup v2 limits: memory.max, cpu.weight, pids.max
Root filesystem: OverlayFS (lower=image layers, upper=container writes)
pivot_root: container process sees isolated / (not host /)
OCI runtime: runc (same syscalls as our Go lab, production-hardened)
Our Go runtime implements steps 3-5 directly via syscall.
Testcontainers adds steps 1-2: Docker API + image pull. What surprised me
The OverlayFS performance surprised me. Mounting an overlay filesystem with 3 image layers takes ~37ms — not because of any I/O, but because the kernel must validate all layer paths, set up the dentry cache entries, and initialize the overlay superblock. Our namespace-only container starts in 8ms. Every Docker container you’ve ever run paid this 37ms overlay tax before the process even started. The first optimization containerd makes is sharing the lower layers across containers, amortizing the mount cost.
The second surprise was how fragile the pivot_root prerequisites are. The kernel requires that newRoot is a mount point (not just a directory), that putOldDir is inside newRoot, and that neither is on a shared mount. Miss any of these and pivot_root returns EINVAL with no indication of which precondition failed. The bind-mount-onto-itself trick (MS_BIND) is required specifically because a plain directory is not a mount point. This is not documented clearly in the pivot_root man page — you learn it by reading runc’s source code.
The third surprise was cgroup cleanup. The kernel refuses to rmdir a cgroup that has ever had the memory controller enabled until all processes are gone, which is obvious. What is not obvious is that rmdir also fails if the cgroup has child cgroups — even empty ones. If a process inside the container forked and that fork created a nested cgroup subtree, you must recursively remove the leaf directories first. Production runtimes do this recursively; our toy does not.
Where to go deeper
- Liz Rice “Containers From Scratch” (video): a 40-minute live coding session that builds a container runtime in Go, covering namespaces and cgroups in real time. The closest thing to this lab in video form.
- runc source code (
libcontainer/): the reference OCI runtime in Go. Thecontainer_linux.gofile is the authoritative implementation of the initialization sequence this lab teaches. - OCI Runtime Specification: defines what any compliant container runtime must implement. The state machine section shows the 7 container states and valid transitions.
- Linux
namespaces(7)man page: the authoritative reference for all 8 Linux namespace types (UTS, PID, mount, network, user, IPC, cgroup, time). The “Restrictions” sections explain why certain operations require specific capabilities. - “Linux Containers Under the Hood” (Aqua Security blog): a detailed walkthrough of the cgroup v2 file interface and how it differs from cgroup v1.
Related labs and questions
Next labs to build:
- Network namespace lab — implement
CLONE_NEWNET, create veth pairs, configure NAT with iptables, and give containers internet access. This is whatdocker network createdoes. - OCI image layer extractor — implement the OCI image spec: pull a manifest from a registry, decompress layer tarballs, apply whiteout files, and produce the directory tree our runtime expects.
- Seccomp filter lab — write a BPF program that filters syscalls and apply it to a container process via
prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, ...). - Service discovery — run multiple containers from this runtime and implement a simple DNS-based service registry so they can find each other by name.
Questions to investigate:
- What happens if you
SethostnamewithoutCLONE_NEWUTS? The call succeeds but changes the host’s hostname — try it in a VM and observe the effect. - Can a process with
CAP_SYS_CHROOTescape achrootjail? If yes, how? Why doespivot_rootprevent the same escape? - What is the maximum nesting depth of PID namespaces? (The kernel has a limit — look it up and test it.)
- If two containers share the same image layers (same lowerdir), and one container modifies a file, does the other container see the modification? Why or why not?
- The
pids.maxlimit is inherited by all child processes in the cgroup. What happens when a shell inside the container tries to run a pipeline (which forks two processes)?