Sub topic 7
Control groups: what a process can use
This chapter assumes you know what a process is, can use a Linux or macOS shell, and have read the previous chapter on namespaces. Namespaces limit what a process can see. Control groups, called cgroups, limit what it can use.
What a cgroup is
A cgroup is a kernel feature that puts processes into a group and applies resource rules to that group. The kernel tracks the group's total usage and enforces the limits. A container runtime creates one cgroup per container and places the container's processes in it.
Typical controllers (the kernel components that manage one resource type) include:
| Controller | Resource | Effect of the limit |
|---|---|---|
| memory | RAM | The kernel kills a process when the group exceeds the limit |
| cpu | CPU time | The kernel throttles the group, which slows it down but does not kill it |
| io | Disk bandwidth | I/O requests are throttled |
| pids | Process count | New processes cannot be created past the limit |
Setting limits with Docker
Docker translates its flags into cgroup settings. This example starts a container limited to 256 MB of memory and half a CPU, then reads the configured values back.
docker run -d --name limited --memory=256m --cpus=0.5 alpine sleep 300
docker inspect --format '{{.HostConfig.Memory}} {{.HostConfig.NanoCpus}}' limited
docker rm -f limitedThe inspect command prints 268435456 500000000. The first number is 256 MiB in bytes. The second is half a CPU in billionths of a CPU.
Reading the cgroup files
On a host that uses cgroup v2, each cgroup is a directory of plain files under /sys/fs/cgroup. From inside a container you see only its own cgroup:
docker run -d --name limited --memory=256m --cpus=0.5 alpine sleep 300
docker exec limited cat /sys/fs/cgroup/memory.max
docker exec limited cat /sys/fs/cgroup/cpu.max
docker rm -f limitedYou should see 268435456 and 50000 100000. The second value means the group may run for 50,000 microseconds in every 100,000 microsecond period. That is half a CPU.
On older hosts that use cgroup v1, the file names and paths differ. The idea is the same.
Common errors
- Exit code 137. A container that exceeds
--memoryis killed by the kernel's out-of-memory (OOM) killer. Docker reports exit code 137, which is 128 plus signal 9. Raise the limit or fix the leak. - Slow but alive. A CPU limit never kills a process. It throttles it. A container that seems slow under load may be hitting its
cpu.maxquota. - Limit versus reservation.
--cpus=0.5is a ceiling, not a guarantee. It does not reserve half a CPU for the container. - No limits by default. Without flags, a container may use all host memory and CPU. One runaway container can starve the others.
- Wrong reading of free memory. Tools that read
/proc/meminfocan show the host's memory instead of the container's limit. Programs that size themselves from it may allocate too much. Checkmemory.maxinstead.
How this compares with VMs
A VM gets a fixed slice of memory and virtual CPUs from its hypervisor, and its guest kernel manages them. A cgroup limit is a rule applied by the one shared host kernel. That makes limits cheap and adjustable on a running container, but it also means the kernel's scheduler and memory manager are shared by every container.
Exercise
A container started with --memory=128m keeps exiting with code 137, while another container started with --cpus=0.25 is slow but never exits. Why do the two limits behave differently?
Show answer
Memory cannot be throttled. When the group exceeds memory.max and the kernel cannot reclaim memory, it kills a process, which gives exit code 137 (128 + SIGKILL). CPU time can be rationed, so the kernel pauses the group once it uses its quota in each period (cpu.max). The process slows down but is not killed.