Containers refresher: PIDs, namespaces, cgroups and syscalls
Overview
Why this post
I keep forgetting how containers work at the kernel level. This is my personal reference. Not an intro for beginners, just the stuff I need to remember.
PIDs
Every process on Linux gets a Process ID (PID). The first process is
systemd (PID 1). Everything else is a child of that.
1pstree # see the full process tree
2ps aux # list all processes
When you run npm start or python main.py, this happens:
- Your shell asks the kernel for a new process with
clone. The kernel copies the shell. The copy gets its own PID and is a child of the shell. - The copy replaces itself with the program you asked for, using
execve. Same PID, new code: it is nownpmorpython. - That program can start children of its own.
npmtypically startssh, which startsnode.python main.pyusually has no extrashin between.
1bash (PID 1500)
2 โโโ npm (PID 1600)
3 โโโ sh (PID 1610)
4 โโโ node (PID 1620)
A process is what the kernel tracks for one running program: a PID, its own memory, one or more threads, open files and sockets, environment variables and a user. It also records the namespaces and cgroup the process belongs to. A child inherits these from its parent, so every process ends up in a set of namespaces without asking for it.
Some things that are easy to mix up:
- A process is one running instance of a program. Start the same binary ten times and you get ten processes.
- Every child is its own process.
npm,shandnodeeach have their own PID, memory and open files. They are three processes, not parts of one. - Threads are not processes. One process can have many threads, for example Node.js with its event loop and the libuv thread pool. Threads share the memory and file descriptors of their process.
- A process can change binary. With
execveit keeps its PID but runs different code.
A namespace is a kernel feature that gives a process its own view of a
system resource, like the process tree or the network. You can see which ones a
process is in with ls /proc/PID/ns. A container is just a process with its
own set.
PIDs matter for containers because namespaces create isolated process trees. Inside a container, the first process gets PID 1, even though it has a different PID on the host.
1# outside the container
2sudo ps -ax | grep bash
3# you'll see the real PID, e.g. 23540
4
5# inside the container
6ps aux
7# you'll see PID 1 (or a low number)
These are the same process, just seen from different namespaces.
Namespaces
Namespaces isolate what a process can see. There are 8:
| Namespace | Isolates |
|---|---|
| PID | Process IDs |
| Network | Network stack, IP addresses, ports |
| Mount (mnt) | Filesystem mount points |
| UTS | Hostname and domain name |
| IPC | Interprocess communication |
| User | User and group IDs |
| Cgroup | Cgroup root directory |
| Time | Clock offsets (monotonic, boottime) |
A process always lives in exactly one of each namespace type. A shell running on the host has 8 namespaces. It just uses the default (host) ones.
1lsns # list all namespaces
2ls /proc/PID/ns # see which namespaces a process uses
Key things to remember:
- PID namespace: nested process trees. PID 1 inside the container is special: killing it kills everything in that namespace.
- Network namespace: its own routing table, IP addresses, and ports. Containers get private IPs (10.x, 172.16.x, 192.168.x) because they are not directly on the public internet.
- User namespace: root inside the container can be mapped to an unprivileged user on the host. Not all container runtimes use this.
- Mount namespace: existed before containers. Originally created so some processes could have mounted storage without other processes seeing it.
1unshare --net bash # start bash in a new network namespace
2nsenter -t PID --all bash # enter all namespaces of a running process
Cgroups
Namespaces control what you can see. Cgroups control what you can use.
Cgroups (control groups) are a kernel mechanism to set resource limits on processes. Originally from Google, merged into the kernel in 2007.
What cgroups control:
- CPU shares
- Memory limits
- Block device I/O
- Network packet classification
1# see cgroup info for a process
2cat /proc/PID/cgroup
3
4# see resource usage
5ls /sys/fs/cgroup
What happens when you hit a limit:
- Too much memory: OOM killer terminates the process.
- Too much CPU: the process gets throttled, not killed.
Cgroups are a last line of defense before the system crashes. Setting limits explicitly is better than relying on the OOM killer.
Syscalls
Everything in Linux happens through system calls, the interface between a process and the kernel.
1program โ syscall โ kernel โ hardware
Containers are created with three key syscalls:
| Syscall | What it does |
|---|---|
clone |
Create a new process, optionally in new namespaces |
unshare |
Move the current process into new namespaces |
setns |
Join an existing namespace |
Example: docker run under the hood calls clone with flags like
CLONE_NEWPID, CLONE_NEWNET, CLONE_NEWNS to create new namespaces for the
container process.
Security: containers limit which syscalls a process can make using
seccomp-BPF. Docker blocks dozens of dangerous syscalls by default. You can
also limit permissions with capabilities. For example, CAP_NET_ADMIN lets
a process change network settings.
A minimal container from scratch in Go: https://github.com/lizrice/containers-from-scratch
How it fits together: docker run
1docker run --rm -it ubuntu /bin/bash
What actually happens:
- Docker downloads the image (a tarball of a filesystem, one per layer).
- It unpacks the layers using an overlay filesystem. Layers are read-only, writes go to a temporary upper layer.
- It calls
pivot_root(orchroot) to make the unpacked filesystem the root for the new process. - It creates new namespaces via
clone: PID, network, mount, UTS, IPC. - It creates a cgroup and assigns the process to it, setting memory and CPU limits.
- It applies a seccomp-BPF filter to block dangerous syscalls.
- It drops capabilities the container does not need.
/bin/bashstarts as PID 1 inside the container.
Quick reference
1# see namespaces of a process
2lsns -p PID
3
4# create a new network namespace
5unshare --net bash
6
7# see cgroup limits
8cat /sys/fs/cgroup/memory/docker/CONTAINER_ID/memory.limit_in_bytes
9
10# see overlay filesystem mounts inside a container
11mount -t overlay
12
13# list capabilities of a process
14getpcaps PID
Sources
- Julia Evans: Containers from scratch zine
- Liz Rice: containers-from-scratch
- NGINX blog: What Are Namespaces and cgroups
- Julia Evans: What even is a container