knaxuslearn · build · ship

Projects / Container runtime

Linux · about 10 hours · 4 phases

Build a container runtime in C

Every docker run ends in a handful of Linux system calls. A container is not a small virtual machine, and the kernel has no "container" object at all. What you get is an ordinary process that has been given its own view of the machine: its own hostname, its own process numbers, its own file system, its own network, fewer rights and a few limits. That sounds like magic until you build it. In about 10 hours you will write a small container runtime in C, one layer at a time, and every layer ends with something you can run.

You work in four phases of about 2.5 hours each. By the end you will have a runtime called rt, about 220 lines of C, that:

  • starts any program with its own hostname and its own process numbers,
  • runs it inside a separate root file system, so the host's files are out of sight,
  • takes away most of root's powers and puts limits on memory, CPU time and open files,
  • gives every run a fresh writable layer on top of a shared read-only image, a private network with only a loopback device, and a proper process number 1 that cleans up after orphans.

You need basic C (structs, pointers, calling system functions and checking their results), a terminal and a Linux machine where you can use sudo. Namespaces, capabilities and the other kernel words are explained when they first show up.

Take a snapshot of your virtual machine before you start, so you can roll back if a step goes wrong. Several steps mount file systems and need sudo.

The four phases

  1. Separate the view

    You end with a program, rt, that runs any command with its own hostname, its own process numbers and its own list of mounts.

    • 1.1 What a container really is30 min
    • 1.2 clone, and what the child does first45 min
    • 1.3 Build it and run it40 min
    • 1.4 Prove it, then break it35 min
  2. A root file system of its own

    You end with commands that run inside a small, separate file system, with working /proc, /dev and /tmp, and nothing of the host visible.

    • 2.1 What goes into a root file system40 min
    • 2.2 Enter it with pivot_root45 min
    • 2.3 /proc, /dev and /tmp35 min
    • 2.4 Run it, and see what is still wrong30 min
  3. Fewer rights, and limits

    You end with a container whose root has only a handful of powers, that can never gain more, and that is stopped when it uses too much memory or CPU time.

    • 3.1 What "root" really means30 min
    • 3.2 Drop the capabilities40 min
    • 3.3 No new privileges, and limits45 min
    • 3.4 Two programs that misbehave35 min
  4. Layers, a network of its own, and a real init

    You end with a runtime that gives each run a throwaway writable layer on a shared image, a private loopback network, and a process number 1 that reaps orphans, plus a script that checks all of it.

    • 4.1 Layers with overlayfs40 min
    • 4.2 A network of its own40 min
    • 4.3 A real process number 140 min
    • 4.4 Put it together30 min

Start with the setup →