Part 3 of 7 · 3 min read
Build a container
Think of a stage crew that builds a small set for one play: a floor, a few walls, props from the store. The play runs inside, and the audience sees only the set. The analogy stops in one place: a crew builds a set from new material, and our set borrows its tools from the host's real files.
In plain words: The recipe has two parts. First, a root file system: a directory with the files the program needs. Here it holds a shell, ls, cat, ps, hostname, mount and the libraries that they need, copied from the host. Second, a launcher: it calls clone with four namespace flags, and in the child it sets the host name, changes the root with chroot (Level 10), mounts a fresh /proc so that ps sees the new PID namespace, and runs the command.
A worked example. The root directory is built with cp and ldd (7.7 MB). The launcher:
#define _GNU_SOURCE
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <unistd.h>
static char stack[1 << 20];
static int child(void *arg) {
char *cmd = arg;
sethostname("tiny", 4);
chroot("/tmp/rootfs");
chdir("/");
mount("proc", "/proc", "proc", 0, NULL);
execl("/bin/sh", "sh", "-c", cmd, (char *)0);
return 1;
}
int main(int argc, char **argv) {
if (argc < 2) { fprintf(stderr, "usage: container COMMAND\n"); return 2; }
int flags = CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWNET | SIGCHLD;
pid_t pid = clone(child, stack + sizeof stack, flags, argv[1]);
if (pid < 0) { perror("clone"); return 1; }
printf("host sees the container as pid %d\n", (int)pid);
fflush(stdout);
waitpid(pid, NULL, 0);
return 0;
}
Running it with a command:
$ ./container 'echo pid=$$ host=$(hostname); ls /; ps -e; cat /etc/os-release'
host sees the container as pid 537
pid=1 host=tiny
bin
etc
lib
lib64
proc
PID TTY TIME CMD
1 ? 00:00:00 sh
4 ? 00:00:00 ps
NAME="Tiny rootfs"
The host sees the container's first process as 537, and the container sees itself as 1. The file system is the six directories of the root, with nothing of the host's /home or /etc. ps lists only two processes, the shell and itself, because of the new PID namespace and a /proc mounted inside it. And it started in 3 milliseconds (measured three times: 0.003 s each), while a VM takes seconds to boot a kernel.
The key lines. There is no special container system call. clone, chroot, mount and execl do everything. A container is a way of using the ordinary calls together.
Root file system (container image)
A root file system, in a container, is the directory tree that becomes / for the container, and holds the programs, libraries and data that its processes need. A container image is a packaged root file system with metadata, stored as layers.
Example: the 7.7 MB tree of six programs and their libraries is the root file system of the model container.
Check yourself:
1. Which call gives the container its own process numbers?
2. Why does the launcher mount /proc inside?
3. What does the container see in /?