Sub topic 10
Under the hood: --cpu-period and --cpu-quota
This chapter assumes you have used --cpus and know that Docker enforces limits through Linux cgroups. You should be comfortable reading docker inspect output.
What `--cpus` really does
The --cpus flag is a convenience. Docker translates it into two lower-level values used by the kernel's Completely Fair Scheduler (CFS) bandwidth control:
- Period: a length of time, in microseconds, that the scheduler uses as a accounting window. The default is 100000 (100 ms).
- Quota: the total CPU time, in microseconds, that all processes in the container may use during one period.
The container's CPU cap is quota / period. With the default period, --cpus 0.5 becomes a quota of 50000 microseconds per 100000.
docker run -d --name half --cpus 0.5 nginx:alpine
docker run -d --name half-manual --cpu-period 100000 --cpu-quota 50000 nginx:alpineThese two containers have the same limit. Use --cpu-period and --cpu-quota directly only when you need to control the period. Docker rejects --cpus together with either of the low-level flags, so pick one style per container.
Quota can exceed one period
The quota counts CPU time across all cores. A container with several busy threads can spend its quota faster than the wall clock advances.
docker run -d --name two-cores --cpu-period 100000 --cpu-quota 200000 nginx:alpineThis is equivalent to --cpus 2. Within each 100 ms window, the container may use 200 ms of CPU time in total, for example two threads running on two cores for the whole window.
What happens when the quota runs out
Once the container has used its quota, the kernel throttles it. Its threads stop running until the next period begins. The container is not killed, which is the key difference from a memory limit. It just becomes slower, and the delay appears as pauses of up to the rest of the period.
A smaller period makes throttling pauses shorter but more frequent. A larger period allows longer bursts and longer pauses. For latency-sensitive services, long pauses can show up as slow requests even when average CPU use looks low.
Reading the values back
Docker records the low-level values in the container's host configuration. If you used --cpus, NanoCpus is set. If you used the low-level flags, CpuPeriod and CpuQuota are set.
docker inspect --format 'period={{.HostConfig.CpuPeriod}} quota={{.HostConfig.CpuQuota}}' half-manual
docker inspect --format 'nanocpus={{.HostConfig.NanoCpus}}' halfTo see throttling as the kernel reports it, read the cgroup's cpu.stat file from inside a container on a cgroup v2 host:
docker exec half-manual cat /sys/fs/cgroup/cpu.statLook at nr_throttled, the number of periods in which the container hit its quota, and throttled_usec, the total time it spent paused. Rising numbers mean the limit is too low for the workload. On cgroup v1 hosts the file is /sys/fs/cgroup/cpu/cpu.stat.
Common errors
- Setting
--cpu-quotawithout--cpu-periodworks, because the period defaults to 100000. Check that your quota makes sense against that default. - Combining
--cpuswith--cpu-periodor--cpu-quotaproduces an error. Use one or the other. - A quota below 1000 microseconds is rejected.
Exercise
A container runs with --cpu-period 50000 --cpu-quota 25000. What is its CPU limit in cores, and what happens when its threads use all 25000 microseconds in a period?
Show answer
The limit is 25000 / 50000 = 0.5 cores. When the quota is used up, the kernel throttles the container's threads until the next 50 ms period starts. The container is not killed.