A minimal container runtime written in C, built from scratch to understand what tools like Docker and runc actually do under the hood.
It uses the same Linux kernel features real container runtimes rely on: namespaces, pivot_root and cgroups v2. No Docker, no libraries, just system calls.
This is a learning project, not a production runtime. See Limitations.
When you run minicontainer, your command starts inside an isolated environment:
| Isolation | Kernel feature | What the process sees |
|---|---|---|
| Hostname | UTS namespace (CLONE_NEWUTS) |
its own hostname: minicontainer |
| Processes | PID namespace (CLONE_NEWPID) |
it is PID 1; host processes are invisible |
| Filesystem | mount namespace (CLONE_NEWNS) + pivot_root |
a full Debian root filesystem, with no path back to the host's files |
| Process info | a fresh procfs mounted inside |
/proc lists only the container's processes |
| Resources | cgroups v2 | memory and number-of-processes limits |
host inside the container
$ hostname → mylaptop # hostname → minicontainer
$ echo $$ → 48213 # echo $$ → 1
$ cat /etc/os-release → Ubuntu # cat /etc/os-release → Debian 12
PARENT (host) CHILD (new UTS + PID + mount namespaces)
1. create /sys/fs/cgroup/minicontainer
memory.max = 100M, pids.max = 20
2. pipe()
3. clone(entrypoint, ..., config) ────────────▶ wait on the pipe
4. write the child's PID → cgroup.procs
5. write "go" to the pipe ────────────────────▶ sethostname
make all mounts private
bind-mount rootfs, pivot_root
mount a new /proc
detach the old root
execvp(command) → PID 1
6. waitpid ◀───────────────────────────────── the command exits (or is killed)
7. remove the cgroup, report how it ended
clone()with a separate stack. The child starts in a new function (entrypoint) with its own stack. The stack grows downward, soclone()receives the top of the allocated block.- A sync pipe. The child waits until the parent has placed it in the cgroup, so the limits apply before the user's command runs. If the parent closes the pipe without writing, the child aborts.
- Mounts are made private first. On systemd hosts
/is a shared mount; withoutMS_REC | MS_PRIVATE, mounts made in the container propagate back to the host (see Lessons learned). pivot_root, notchroot.pivot_rootmoves the whole root mount, and once the old root is unmounted there is no path back to the host's filesystem.chrootonly changes path lookups and can be escaped.execvp()last. All isolation is set up first; then the setup code is replaced by the user's command, which keeps PID 1 and inherits every namespace and limit.- All-or-nothing setup. If any step fails, the container exits with status 1. It never starts a command without full isolation.
- Docker-style exit codes. A container killed by a signal returns
128 + signal(e.g.137for SIGKILL from the OOM killer).
.
├── Makefile
├── include/container.h # config struct, constants, prototypes
└── src/
├── main.c # defaults → config → start_process
├── container.c # parent: cgroup, clone, sync pipe, wait, cleanup
├── child.c # child: wait for "go", hostname, rootfs, exec
├── mounts.c # private mounts, pivot_root, /proc, old root
└── cgroups.c # create cgroup, set limits, add PID, remove
- Linux with a real kernel: a VM, or WSL 2 (not WSL 1)
- root privileges (
sudo) - cgroups v2 mounted at
/sys/fs/cgroup, with thememoryandpidscontrollers enabled gccandmake- Docker (only to create the root filesystem, see below)
⚠️ Run it in a VM or WSL 2, not on your main system. It performs mounts and cgroup operations as root. A bug in this kind of code can affect the whole machine it runs on (see Lessons learned).
Check cgroups v2:
mount | grep cgroup2 # cgroup2 on /sys/fs/cgroup
cat /sys/fs/cgroup/cgroup.subtree_control # must include: memory pids1. Create a root filesystem from a Debian image:
mkdir rootfs
sudo docker export "$(sudo docker create debian:bookworm)" | sudo tar -x -C rootfsdocker export flattens all of the image's layers into a single tar archive, which is unpacked into a plain folder.
2. Set its absolute path in include/container.h:
realpath rootfs# define ROOTFS "/absolute/path/to/rootfs"3. Build:
makesudo ./container # interactive shell (/bin/sh) inside the container
sudo ./container cat /etc/os-release # run a single commandDefaults are set in include/container.h:
| Setting | Default |
|---|---|
| hostname | minicontainer |
| command | /bin/sh |
| memory limit | 100M |
| process limit | 20 |
Inside the container:
hostname # minicontainer
cat /etc/os-release # Debian 12 (bookworm): the rootfs, not the host
ls /home # empty: the host's files are not visible
ls /.oldroot # No such file or directory: the old root was detached
echo $$ # 1
ls /proc | grep -E '^[0-9]+$' # only the container's PIDsAfter exit, on the host:
ls /sys/fs/cgroup/minicontainer # No such file or directory: cgroup cleaned up
ls rootfs/ # no .oldroot left behind
ps aux | head -3 # host processes unaffectedMemory (the string doubles until it exceeds 100 MB):
sudo ./container sh -c 'a=x; while true; do a="$a$a"; done'; echo "exit=$?"
# expected: container killed by signal 9 → exit=137Processes (tries to start 40, the limit is 20):
sudo ./container sh -c 'for i in $(seq 1 40); do sleep 30 & done; wait'
# expected: "Cannot fork" / "Resource temporarily unavailable"While a container runs, its limits are visible from the host:
cat /sys/fs/cgroup/minicontainer/memory.max # 104857600
cat /sys/fs/cgroup/minicontainer/pids.max # 20
cat /sys/fs/cgroup/minicontainer/cgroup.procs # the container's processes (host PIDs)Mount propagation. In an early version, /proc was mounted before the mounts were made private. Because / is a shared mount on systemd systems, the new procfs propagated to the host and covered its real /proc. On the host, /proc then showed only the (finished) container's processes: desktop icons disappeared and applications refused to start. A reboot restored everything, since mounts live only in memory.
The fix is a single line, run before any other mount:
mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL);CLONE_NEWNS gives the child a copy of the mount table, but shared mounts stay linked to the host's. Only making them private makes the copy truly independent.
Silent failures are worse than crashes. A version that ignored the return value of the rootfs setup started a shell anyway, on the host's filesystem, looking like a working container with no isolation at all. Every setup step is now checked, and any failure stops the container.
A container is just a process. Same kernel, same scheduler. The isolation is entirely in what the kernel lets the process see (namespaces) and use (cgroups). Inside, the shell is PID 1 and its parent is reported as 0, because the real parent lives outside the namespace it can see.
Compared to Docker or runc, minicontainer does not:
- use image layers or a copy-on-write filesystem: the rootfs is a plain folder, so changes made inside the container are permanent
- create a network namespace: the container shares the host's network
- use a user namespace: it requires root
- drop capabilities or apply seccomp filters
- set up
/dev(beyond what the rootfs provides) or/sys - allocate a pseudo-terminal: an interactive shell may print
Cannot set tty process groupon exit, because the terminal's original process group lives outside the container's PID namespace - support multiple containers at once (the cgroup name is fixed)
- parse command-line options (configuration is set in the header)
- CPU limit (
cpu.max) - Minimal
/dev(tmpfs+null,zero,tty, ...) - Network namespace with a veth pair
- User namespace (rootless containers)
- Capabilities and seccomp
-
execinto a running container withsetns() - Command-line options (
--rootfs,--mem,--pids, ...)
- Liz Rice, Containers From Scratch (GOTO 2018): https://www.youtube.com/watch?v=8fi7uSYlOdc
man 7 namespaces,man 7 mount_namespaces,man 7 cgroupsman 2 clone,man 2 pivot_root,man 2 mount- Michael Kerrisk, Namespaces in operation (LWN.net article series)
- Linux kernel documentation, Control Group v2