Skip to content

Container From Scratch

You'll run a process in its own view of the filesystem, process table and network using Linux namespaces, chroot and cgroups, in a few hundred lines of C or Rust.

About

A container is a normal process that the kernel has been asked to lie to, and the lies are a handful of system calls. You'll use clone with namespace flags to give a child its own hostname, PID 1 and mount table, chroot it into a small root filesystem, and cap its memory with a cgroup. The whole thing fits in a few hundred lines and demystifies a tool most of us run daily. This one is Linux only, so use a free virtual machine or WSL2 elsewhere, and do it in a VM anyway, because you'll be running as root.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
6
Takes
Three weeks
Skill level
Advanced (years of practice)
Cost
Free
Supplies
You need some
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Systems Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Run a shell in a new UTS namespace and change its hostname without changing the host's. Use clone with CLONE_NEWUTS, then sethostname in the child before it execs a shell. Run hostname inside and in another terminal on the host. Take a screenshot showing the two different names. Photo
  2. 2
    Give the child its own PID namespace so your shell is PID 1 inside. Add CLONE_NEWPID and CLONE_NEWNS, then mount a fresh /proc in the child so ps reads the right table. On a host running systemd, mark the whole mount tree private first (mount with MS_REC and MS_PRIVATE on /) or your fresh /proc shows up on the host too. Run ps inside. Take a screenshot of ps inside showing PID 1 is your shell. Photo
  3. 3
    Build a small root filesystem and chroot the child into it. Download an Alpine mini root tarball, which is a few megabytes, and extract it into a folder. Use chroot or pivot_root in the child, then ls / inside. Take a screenshot of the root listing from inside and of trying to reach a file on the host, which fails. Photo
  4. 4
    Cap the memory with a cgroup and show a greedy program being killed. Create a cgroup under /sys/fs/cgroup (cgroup v2, which current distros use), set memory.max to 50M, and add the child's PID to it. Then run a program inside that allocates and touches 200 MB. Take a screenshot of the kill in dmesg or the exit status. Photo
  5. 5
    Give the child its own network namespace and a veth pair so it can ping the host. Use CLONE_NEWNET, then set up a veth pair with ip link from the host side, move one end into the child's namespace, and bring up both ends with addresses. Ping the host from inside. Take a screenshot of ip addr inside and the ping succeeding. Photo
  6. 6
    Run a real program inside your container and reach it from the host. Install Python or a tiny web server into your Alpine root first. Start it inside, and curl it from the host over the veth link. Write three lines on what Docker adds on top of what you built. Take a screenshot of the curl succeeding and the three lines. Photo