>_ IzzyLab

Hands-on task

This task is step 12 of the course “Containers from the inside: namespaces and cgroups”

A container without Docker: unshare, chroot and a cgroup

hard~25 min

To understand what Docker does, build a container yourself — out of kernel tools. The filesystem of the busybox image is already unpacked into /srv/rootfs, and the service's page is /srv/rootfs/www/index.html.

Run a web server inside it with the command httpd -f -p 8080 -h /www so that it's isolated like in a container:

  • the process's filesystem root is /srv/rootfs;
  • its own PID, mount and UTS namespaces: in its PID namespace httpd is PID 1, /proc inside shows only it, and the hostname is mini;
  • a cgroup mini with limits: 64 MiB of memory and no more than 32 processes;
  • the network is shared with the server: the page opens from the host at http://127.0.0.1:8080/.

Don't use Docker for this.

What you'll practise:
  • namespaces
  • unshare
  • chroot
  • cgroups

Topic breakdown

A container isn't a separate kernel entity but an ordinary process that has been given several isolation mechanisms at once: a new filesystem root (chroot or pivot_root), namespaces of its own (PID, mount, UTS, network, IPC, user) and a cgroup with limits. Docker, containerd and runc do exactly that, plus they download and assemble the image layers, set up the network and drop extra privileges (capabilities, seccomp). unshare can do almost all of it in one command: --pid --fork is a new PID namespace where unshare's child becomes the first process, --mount is a mount tree of its own (where you can mount your own /proc without touching the host), --uts is a hostname of its own, --root is a new root. A process inherits its cgroup from its parent, so the shell is moved into the group before the launch.

What's checked

  1. httpd is running with the root /srv/rootfs
  2. httpd has its own PID, mount and UTS namespaces
  3. In its namespace httpd is PID 1, and /proc inside is its own
  4. The hostname inside is mini
  5. httpd is in the cgroup mini: 64 MiB of memory and up to 32 processes
  6. The page opens from the host

What it looks like