Sub topic 9
Pinning containers to cores with --cpuset-cpus
This chapter assumes you know docker run, can read docker inspect output, and know that a host has numbered CPU cores. It also assumes you have seen --cpus and --cpu-shares in earlier chapters.
What pinning does
--cpus limits how much CPU time a container gets. --cpuset-cpus limits which cores the container may run on. Docker passes the list to the cgroup cpuset controller, and the kernel scheduler then places the container's processes only on those cores.
Cores are numbered from 0. You can give a comma-separated list, a range, or both:
| Value | Cores allowed |
|---|---|
| `0,1` | Core 0 and core 1 |
| `0-3` | Cores 0, 1, 2 and 3 |
| `0,2-3` | Cores 0, 2 and 3 |
Pinning a container
docker run -d --name pinned --cpuset-cpus 0,1 nginx:alpineThis container's processes run only on cores 0 and 1 of the host. If you name a core the host does not have, docker run fails with an error that lists the available CPUs.
Checking that it worked
Docker records the setting in the container's configuration:
docker inspect --format '{{.HostConfig.CpusetCpus}}' pinnedThe output is 0,1. You can also check from inside the container. The kernel reports the allowed cores in the process status file:
docker exec pinned grep Cpus_allowed_list /proc/self/statusThe output shows Cpus_allowed_list: 0-1. The kernel prints a range where it can, so 0,1 appears as 0-1.
What pinning does not do
Pinning restricts where a container runs. It does not reserve those cores for the container. Other containers, and host processes, can still run on cores 0 and 1 unless you pin them elsewhere.
Pinning also does not cap CPU time on its own. A container pinned to two cores can use 100% of both. To cap usage inside the pinned set, combine the options:
docker run -d --name pinned-capped --cpuset-cpus 0,1 --cpus 1.5 nginx:alpineThe container may run only on cores 0 and 1, and it may use at most 1.5 cores' worth of time across them.
When pinning is useful
- Isolating a noisy workload. Pin a batch job to cores 2-3 so it cannot disturb a latency-sensitive service on cores 0-1. Pin both containers, because pinning only one leaves the other free to use every core.
- Cache and NUMA locality. Keeping a process on the same cores avoids moving its data between CPU caches. On multi-socket servers,
--cpuset-memsselects which memory nodes the container uses. Pair it with--cpuset-cpusso the cores and memory sit on the same node. - Repeatable benchmarks. Fixed cores remove one source of variation between runs.
Common mistakes
- Using pinning when you want a quota. If you only need to stop a container from using too much CPU,
--cpusis simpler and lets the scheduler balance load. - Pinning many containers to the same cores by habit, such as always
0,1. Those cores become crowded while the others sit idle. - Hard-coding core numbers in scripts that run on hosts with different core counts.
Exercise
You have a four-core host. A report job must not slow down a web service. You start the web service with --cpuset-cpus 0,1 and the report job with no CPU options. Does this guarantee the web service has cores 0 and 1 to itself? How would you change the report job?
Show answer
No. Pinning restricts only where the web service runs. The unpinned report job can still be scheduled on cores 0 and 1. Pin the report job to the other cores, for example --cpuset-cpus 2,3, so the two sets of cores do not overlap.