Projects / Container runtime / Wrap up
Wrap up
You built the core of a container runtime from the kernel's own parts: new namespaces from clone, a root file system entered with pivot_root, a smaller set of capabilities, no_new_privs and resource limits, an overlay for each run, a private network and a small init. Container tools such as Docker and Podman do these same things, plus a lot more.
What this version leaves out
- There are no cgroups.
RLIMIT_ASandRLIMIT_CPUlimit single processes, and a container with many processes gets the limit many times. - It needs real root. There is no user namespace, so root inside is root of the host, with fewer capabilities.
- There is no
seccompfilter, so every system call that the kept capabilities allow is available. - Sensitive paths under
/procand/sysare not hidden or made read-only. - The image is a directory built from the host's programs. There is no image format, no download and no layer cache.
- The network is only a loopback. There is no connection to the outside, and no DNS.
- If
rtis killed withSIGKILL, it cannot clean up its scratch directory under/tmp. - We ran everything on Linux 6.18 as root. We did not run the guide on other kernels.
Keep going
- Cgroups v2. Create a directory for each run under the cgroup file system, write numbers into
memory.max,cpu.maxandpids.max, and write the container's process number intocgroup.procs. This limits the whole group, which is whatRLIMIT_AScould not do. It needs a machine with cgroup v2 mounted and root access to it. - User namespaces. Add
CLONE_NEWUSERand map user 0 inside to an ordinary user outside, so thatrtcan run withoutsudo. - A real network. Create a virtual Ethernet pair with
ip link, move one end into the container's namespace, give both ends addresses, and add a route and address translation on the host. - Seccomp. Install a filter that returns an error for system calls the container has no business making.
- Run a command in a running container. Keep the namespace handles from
/procand join them withsetns. - More layers. Unpack two or three directories as separate lower layers and list them in
lowerdir.
What real runtimes do differently
- They are driven by a configuration file. The Open Container Initiative runtime specification describes the container as one JSON document, with fields for the process and its capabilities, resource limits and
noNewPrivileges, and a Linux section for namespaces, cgroups and seccomp. Many of our command-line flags are fields there. - They set up user, cgroup and IPC namespaces too, and mask sensitive paths in
/proc. - Images come as stacked layers that are unpacked and mounted as many overlay lower directories, and are shared between containers.
- They connect networking with virtual cables and bridges, set up addresses and DNS, and clean up even after a crash.
Check yourself
Answer each question in your head first, then open it to check.
Why does
psinside the container show only two processes, even though the host runs hundreds?The container has its own PID namespace, so it only sees processes created inside it. Andpsreads/proc, which we mounted again inside the new mount namespace after the clone, so it reflects the new PID namespace instead of the host's.Why do we bind mount the root directory onto itself before calling
pivot_root?pivot_rootneeds the new root to be a mount point, and an ordinary directory is not one. A bind mount of a directory onto itself makes it a mount point without copying anything.We drop capabilities from the bounding set. What does
no_new_privsstop that the dropped capabilities alone do not?A set-uid program, or a file with file capabilities, can raise the rights of the process duringexecup to what the bounding set allows. Withno_new_privs,execcan no longer give more rights than the caller had, so set-uid bits and file capabilities have no effect at all.A teammate says "
--mem 64guarantees the container never uses more than 64 MB". What do you answer?RLIMIT_ASlimits the address space of each process. It counts reserved memory that was never used, and each extra process gets its own full allowance, so the total can be much higher than 64 MB. A limit on actual use by the whole container needs a cgroup, for examplememory.max.Teach it back: someone tells you containers are lightweight virtual machines. What is wrong with that?
A virtual machine runs its own kernel on virtual hardware. A container is a group of ordinary processes on the host kernel, with a restricted view and restricted rights. Everything they do is a system call to the host's kernel, so a kernel bug, or a limit we forgot to set, can reach the host.
Sources
- namespaces(7), the overview of all namespace types and their clone flags.
- clone(2), the system call that starts a process in new namespaces.
- mount_namespaces(7), including the shared, private and slave mount types.
- pid_namespaces(7), on process number 1 and the adoption of orphans.
- network_namespaces(7), on what a network namespace separates and how virtual device pairs connect them.
- pivot_root(2), including the
pivot_root(".", ".")sequence. - capabilities(7) and PR_CAPBSET_DROP, for the list of capabilities and the bounding set.
- No New Privileges Flag, from the kernel documentation.
- getrlimit(2), on soft and hard limits and what each resource does at its limit.
- Overlay Filesystem, the kernel documentation for
lowerdir,upperdirandworkdir. - Control Group v2, for the group limits we left out.
- OCI runtime specification: container configuration and its Linux section.