Sub topic 2
How Docker enforces limits: cgroups
This chapter assumes you can run containers with docker run and read a Linux shell. It also assumes you know what CPU cores, RAM, and swap are. Here you learn which kernel feature enforces the limits, so later chapters on memory and CPU flags make sense.
Docker does not enforce limits itself
A container is an ordinary Linux process with extra isolation. Docker does not watch it and stop it when it uses too much. The Linux kernel does that work through control groups, usually called cgroups. A cgroup is a kernel object that holds a set of processes. It applies resource rules to all of them together.
When you pass a flag such as --memory 256m, the Docker daemon passes the value down to the container runtime. The runtime creates a cgroup for the container and writes the limit into it. It then starts your process inside that cgroup.
Cgroup versions
Linux has two cgroup versions. Version 1 uses a separate directory tree for each resource type. Version 2 uses one unified tree and is the default on current distributions. Check which one your host uses:
docker info --format '{{.CgroupVersion}}'The command prints 1 or 2. The examples in this tutorial use version 2 file names.
Flags map to cgroup files
Each Docker flag becomes a value in a file inside the container's cgroup directory.
| Docker flag | cgroup v2 file | Effect |
|---|---|---|
| `--memory` | `memory.max` | Hard memory limit |
| `--memory-swap` | `memory.swap.max` | Swap allowance (Docker subtracts the memory limit from the total) |
| `--memory-reservation` | `memory.low` | Soft limit, applied under memory pressure |
| `--cpus` | `cpu.max` | CPU quota per period |
| `--cpu-shares` | `cpu.weight` | Relative CPU weight (Docker converts the value) |
| `--cpuset-cpus` | `cpuset.cpus` | Allowed cores |
Later chapters cover each flag in detail. For now, remember that the mapping is direct, so you can always read the kernel's view of a limit.
Reading the limits from inside a container
On a cgroup v2 host, a container sees its own cgroup at /sys/fs/cgroup. Start a limited container and print two of the files:
docker run --rm --memory 256m --cpus 0.5 alpine \
sh -c 'cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max'The output is:
268435456
50000 100000The first line is 256 MiB in bytes. The second line means the container may use 50,000 microseconds of CPU time in every 100,000 microsecond period. That is half of one core. A container with no limit shows max in these files.
Where the cgroup lives on the host
On the host, the cgroup directory depends on Docker's cgroup driver. With the systemd driver, the path looks like /sys/fs/cgroup/system.slice/docker-<container-id>.scope/. With the cgroupfs driver, it looks like /sys/fs/cgroup/docker/<container-id>/. Check the driver with docker info --format '{{.CgroupDriver}}'.
Common errors
- Looking for files in the wrong place. Paths differ between cgroup v1 and v2, and between drivers. Check the version and driver first.
- Assuming the limit is a Docker feature. If a container is killed for using too much memory, the kernel did it, not the Docker daemon. The next chapters show how to detect this.
- Expecting a limit to appear when you set none. Docker applies no limits by default, so the cgroup files show
max.
Exercise
You run a container with --cpus 1.5 on a cgroup v2 host. Which cgroup file holds the limit, and what would it contain if the period is the default 100,000 microseconds?
Show answer
The limit is in cpu.max. It would contain 150000 100000, meaning 150,000 microseconds of CPU time per 100,000 microsecond period, which is one and a half cores.