Curious Pandas

A process that thinks it is alone

A container isn't a tiny virtual machine. It's an ordinary process the kernel lies to about what it can see, and limits in what it can use.

12 min+30 XP for finishing

People say “a container is a lightweight virtual machine”. It’s a handy picture and it’s wrong in a way that will bite you. A virtual machine runs its own kernel on pretend hardware. A container runs on your kernel, the same one every other process on the machine uses. It’s just a process.

What makes it feel like a separate machine are two Linux kernel features, applied to that process:

  • Namespaces change what the process can see. In its own PID namespace it’s process number 1 and can’t see anyone else’s processes. In its own mount namespace it sees a different root filesystem. In its own network namespace it has its own interfaces and ports. The manual page puts it this way: a namespace wraps a global resource so that processes inside it appear to have their own isolated instance of it.
  • cgroups (control groups) limit what the process can use: how much memory, how much CPU time, how many processes. Go over a memory limit and the kernel’s out-of-memory killer ends the process.
Namespace Makes the process think it has its own…
PID process list (and it is PID 1)
Mount filesystem tree
Network network interfaces, IP addresses, ports
UTS hostname
IPC shared memory and message queues
User user and group IDs (root inside, nobody outside)
Cgroup view of the cgroup hierarchy
Time boot and monotonic clocks

The image is the other half: a bundle of files (a whole minimal Linux userland plus your app) and some settings, like which command to run. A container is an image’s files, unpacked into a mount namespace, with one process started inside it.

Check yourself+10 XP

You run `ps aux` on the host while a container is running nginx. What do you see?

Why the difference matters

Because every container shares the host’s kernel:

  • A Linux container needs a Linux kernel. On a Mac or Windows laptop, Docker Desktop quietly runs a small Linux virtual machine and runs your containers inside it.
  • A memory limit is enforced by that shared kernel. The out-of-memory kill you’ll meet in the Kubernetes track, OOMKilled, exit code 137, is a cgroup doing exactly its job.
  • Isolation is good but not a VM boundary. A kernel bug is a bug for every container on the machine at once, which is why people running untrusted code add sandboxes or real VMs.
Check yourself+10 XP

A container has a 64 MiB memory limit and its process tries to use 200 MiB. What enforces the limit?

Check yourself+10 XP

Can a container built for Linux run directly on the Windows kernel?

Do it for real · your machine+50 XP bonus

Everything above ran in your browser. Now do it on a real cluster withthe tilt platform. You need Docker, kind and Tilt, about 15 GB of free disk and 8 GB of spare RAM. Setup guide · Can my machine run it?

With Docker installed, see the narrowed view from inside and the plain process from outside:

docker run --rm -d --name panda alpine sleep 300
docker exec panda ps            # inside: sleep is PID 1; the only other process is ps itself
docker exec panda hostname      # its own hostname (UTS namespace)
docker top panda                # outside: the same process, with its host PID
docker rm -f panda
Self-reported. We can't see your machine, and we don't want to. This is a bonus, never required.

Sources

Finished reading?

Missions and quizzes above earn their own XP. This marks the lesson done.