What a container really is

Intermediate · 43 min

A container is not a small VM. It is an ordinary Linux process with namespaces limiting what it sees, cgroups limiting what it uses, and its own filesystem.

Before thisThe Linux server from an operator's seatRunning services with systemd

GOTO Conferences · 43 min

What a container really is

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by GOTO ConferencesReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A container is a normal process on the host's Linux kernel. There is no guest operating system and no hypervisor in between.
  • Namespaces control what the process can see: its own process IDs, hostname, network interfaces and mount points.
  • Control groups (cgroups) control what it can use: memory, CPU and the number of processes, enforced by the kernel.
  • The image supplies the root filesystem and a little configuration, such as the command to run. It contains no kernel, which is why a Linux image needs a Linux kernel.
  • Because every container shares one kernel, isolation is weaker than a virtual machine's, so running as a non-root user and limiting privileges still matters.

“Works on my machine”

The API runs on your laptop with Python 3.13 and a particular build of a database library. The server has Python 3.11 and a different library. A colleague’s machine has something else again. Every difference is a bug waiting for a deploy.

One fix is a virtual machine per app: ship a whole operating system with the app inside. That works, but each VM boots its own kernel, reserves its own memory and takes a while to start. Containers solve the same problem with much less. To use them well, and to debug them, you need to know what they actually are, because the common phrase “lightweight VM” is wrong in ways that matter.

A process with blinkers on

A container is an ordinary Linux process, started by the host’s kernel like any other. Three kernel features make it look like a separate machine to itself.

Namespaces limit what a process can see. Each kind isolates one thing:

Namespace The process gets its own
PID process numbering, so it can be PID 1
mount set of mounted filesystems, so it sees its own /
network network interfaces, routes and ports
UTS hostname
IPC, user, cgroup message queues, user IDs, cgroup view

Control groups limit what a process can use: memory, CPU time, number of processes. They are the same kernel feature systemd uses to track a service in the systemd lesson. When a container exceeds its memory limit, the kernel’s out-of-memory killer stops a process in it.

A root filesystem from an image gives the process its own /, with its own libraries and binaries, so it does not care what is installed on the host.

Think of an open-plan office with partitions. Everyone shares the building’s power and air, which is the kernel. Partitions control what each team can see, the namespaces. The facilities manager caps each team’s share of the electricity, the cgroups. A VM is a separate building.

What the image gives you

I had no Linux machine and no Docker on this Mac, so I fetched the official nginx image’s pieces from Docker Hub’s registry API with curl and looked inside. The image config holds what to run (jq’s output, with the arrays folded onto one line each):

$ jq '{Entrypoint:.config.Entrypoint, Cmd:.config.Cmd, ExposedPorts:.config.ExposedPorts, StopSignal:.config.StopSignal}' config.json
{
  "Entrypoint": ["/docker-entrypoint.sh"],
  "Cmd": ["nginx", "-g", "daemon off;"],
  "ExposedPorts": { "80/tcp": {} },
  "StopSignal": "SIGQUIT"
}

The rest is files. I unpacked the layers:

$ ls rootfs/boot | wc -l
       0
$ file l2/usr/sbin/nginx | tr ',' '\n' | head -5
l2/usr/sbin/nginx: ELF 64-bit LSB pie executable
 x86-64
 version 1 (SYSV)
 dynamically linked
 interpreter /lib64/ld-linux-x86-64.so.2
$ uname -sm
Darwin arm64

/boot is empty: there is no kernel in an image. The nginx binary is a Linux x86-64 program that expects Linux’s dynamic loader. This Mac is Darwin on ARM, so it cannot run that binary directly, and I did not try. That is why Docker Desktop on a Mac quietly runs a Linux VM and starts containers inside it. On a Linux server there is no VM: the container’s processes are the host’s processes.

Notice daemon off; too. In a container, the main process must stay in the foreground, because when PID 1 exits the container stops. That is the opposite of the Type=forking nginx unit in the systemd lesson, which expects nginx to fork into the background.

Seeing it on Linux

These are the commands I would use on a Linux host to see the pieces. I did not run them, since I had no Linux machine, so the descriptions come from the man pages and kernel docs, not from output.

sudo unshare --pid --fork --mount-proc --uts bash   # a new PID and hostname namespace
ps -ef                     # inside: bash is PID 1 and few other processes appear
hostname box-1             # changes the hostname only inside the namespace

docker run -d --name web --memory 256m nginx
cat /proc/$(docker inspect -f '{{.State.Pid}}' web)/cgroup   # the container's cgroup
# the 256m limit appears as memory.max in that cgroup's directory under /sys/fs/cgroup

unshare is the raw building block. Docker, Podman and Kubernetes all end up asking a low-level runtime, usually runc or crun, to set up these namespaces and cgroups according to the Open Container Initiative’s runtime spec. Most current distributions use cgroup v2, a single unified hierarchy, and older tutorials that show per-controller directories such as /sys/fs/cgroup/memory/ describe v1.

What it means for operations

  • It is just a process. ps on the host shows it, signals reach it, and a graceful shutdown works the same way.
  • One kernel for everyone. A kernel vulnerability can cross containers. Run as a non-root user, drop privileges, and use a VM boundary between tenants who do not trust each other.
  • Limits are real. A memory limit set too low shows up as a process being killed with no application error, which looks mysterious until you check the cgroup.

How the image’s filesystem is built, and why it comes in layers, is the next lesson. Containerizing a backend service shows the developer’s side.

About the video

“Containers From Scratch” is Liz Rice’s talk at GOTO Amsterdam 2018, 42 minutes 53 seconds. The description says she live-codes a container in a few lines of Go to answer what a container is, whether it is a lightweight VM, and what namespaces and control groups do. It is from 2018, so the cgroup details may show the v1 layout. The ideas it builds, and the way it builds them, have not changed. There are no chapters. English was judged from the title, description, channel and its English auto-caption track. Samuel Karp’s talk in deeper also covers image mechanics and runtimes.

Where this fits

You know what runs. Next, how the filesystem it runs from gets built: images, layers and writing a Dockerfile.

Check yourself

  1. 01Inside a container, ps shows your app as PID 1. On the host, ps shows it as PID 48213. How can both be true?

    The container runs in its own PID namespace, which numbers processes from 1. The host sees the same process in the host's namespace, with its real PID.

  2. 02What stops one container from using all of the host's memory?

    A cgroup memory limit, for example memory.max in cgroup v2. If the processes in that group exceed it, the kernel's out-of-memory killer stops one of them.

  3. 03Why does Docker on a Mac run a Linux virtual machine behind the scenes?

    Container images hold Linux binaries and expect a Linux kernel to run them. macOS has a different kernel, so Docker Desktop runs a small Linux VM and starts the containers inside it.

  4. 04Why is a container escape more serious than a bug inside a VM guest?

    Containers share the host kernel. A kernel bug reachable from a container can affect the host and every other container on it, whereas a VM has its own kernel behind a hypervisor.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark