Sub topic 7
Capping CPU with --cpus
This chapter assumes you can run containers with docker run, use a Linux shell, and know what a CPU core is. Memory limits were covered earlier. CPU limits work differently, so keep that contrast in mind.
What --cpus does
The --cpus flag sets a hard ceiling on how much CPU time a container may use. The value is a number of cores. --cpus 0.5 means half of one core. --cpus 2 means the time of two full cores.
docker run -d --name web --memory 256m --cpus 0.5 nginx:alpineThis starts a container that can use at most 256 MiB of memory and half of one CPU core.
Docker enforces the cap with the Linux kernel's CFS bandwidth control, a feature of the Completely Fair Scheduler. The container's cgroup gets a time budget for each scheduling period. Once the container's processes use up the budget, the kernel pauses them until the next period starts.
Throttling, not killing
A container that hits its memory limit can be killed. A container that hits its CPU limit is only slowed down. The kernel pauses its processes, and they resume when the next period begins. Nothing crashes and no exit code is recorded.
| Limit | What happens at the ceiling |
|---|---|
| `--memory` | The kernel may kill the process (out-of-memory kill) |
| `--cpus` | The kernel pauses the process until the next period (throttling) |
Throttling shows up as higher latency and lower throughput. A web server limited to --cpus 0.5 can answer requests slowly under load while its logs show no errors.
A cap applies even when the host is idle
--cpus is a ceiling, not a guarantee. The cap holds even when every other core on the host is idle. A container limited to --cpus 0.5 still runs at half a core on an empty 16-core machine.
The cap also does not reserve any CPU for the container. If other workloads are busy, the container can get less than its limit. To control how contended CPU is divided, use --cpu-shares, which a later chapter covers.
How the quota is spread across cores
The cap is a total time budget, not a core count. With --cpus 2, a multi-threaded process can run on four cores at once, each for half the time. The container never uses more than two cores' worth of time in total.
Checking the result
You can confirm that the limit was recorded and watch usage against it.
docker inspect --format 'mem={{.HostConfig.Memory}} nanocpus={{.HostConfig.NanoCpus}}' web
docker stats --no-stream webNanoCpus stores the limit in billionths of a core. For --cpus 0.5 it shows 500000000. In docker stats, the CPU % column is measured against one core, so a container capped at --cpus 0.5 tops out near 50%.
Common mistakes
- Setting
--cpushigher than the number of cores on the host. Docker rejects the value. - Expecting
--cpusto reserve CPU. It only sets a maximum. - Reading throttling as a bug. Slow responses with no errors are the usual sign of a CPU cap that is too low.
- Using
--cpustogether with--cpu-periodand--cpu-quota. They set the same underlying values, so use only one style.
Exercise
A container runs with --cpus 1 on an idle 8-core host. Its application starts 8 busy threads. How much CPU time can the container use, and what does the kernel do when it hits that limit?
Show answer
The container can use at most one core's worth of CPU time in total, even though the host is idle and the threads can spread across cores. When the budget for a scheduling period is used up, the kernel pauses the container's processes until the next period starts. It throttles them and does not kill them.