Sub topic 8
Sharing CPU Fairly with --cpu-shares
This chapter assumes you can run containers with docker run, know what a CPU core is, and have seen --cpus as a hard CPU cap. Here you will learn a different control: relative CPU weight.
What shares are
--cpu-shares gives a container a relative weight for CPU time. The default is 1024. The value is not a number of cores or a percentage. It only means something when you compare it with the weights of other containers on the same host.
Shares are enforced by the Linux kernel's CPU scheduler through cgroups. They matter only when containers compete for CPU. If the host has idle CPU, a container with a low weight can still use as much as it can run.
A worked example
This starts two containers with different weights.
docker run -d --name heavy --cpu-shares 2048 nginx:alpine
docker run -d --name light --cpu-shares 512 nginx:alpineThe weights are 2048 and 512, so heavy gets four times the weight of light. Under contention, heavy receives about 2048 / (2048 + 512) = 80% of the contended CPU time. light receives about 20%.
Suppose the host is idle except for these two containers, and both run busy loops. They would split the CPU 80/20. If heavy stops working, light can use all the CPU it needs. The weights have no effect when nothing competes.
The weights are shared among every container that is runnable on the host. A third container with the default 1024 would change the split. The percentages are the weight divided by the sum of the weights of all competing containers.
How shares compare with --cpus
The two options solve different problems.
| Option | Type | Effect when the host is idle | Effect under contention |
|---|---|---|---|
| `--cpus 0.5` | Hard cap | Still capped at half a core | Capped at half a core |
| `--cpu-shares 512` | Relative weight | No limit | Smaller portion of CPU than default containers |
Use --cpus when you need a firm ceiling, for example to keep a noisy batch job from using more than one core. Use --cpu-shares when you want to rank containers and let all of them use spare CPU. You can combine them. A container may have a high weight and a ceiling.
Common mistakes
- Reading shares as a quota. A weight of 1024 does not mean one core.
- Expecting shares to protect a container on an idle host. They do not reserve anything.
- Testing with a single container. With no competitor, any value gives the same result.
- Comparing values across hosts. The share a weight produces depends on the other containers that run on that host.
- Forgetting that the weights apply to all workloads in the same cgroup hierarchy, not just Docker containers you started on purpose.
Checking the value
You can see the configured weight with docker inspect.
docker inspect --format '{{.HostConfig.CpuShares}}' heavyThis prints 2048 for the container above. To watch the effect, run a CPU-bound process in each container on a host with few cores, and compare docker stats --no-stream. If the host has many idle cores, you will not see a difference, because no contention exists. To force contention, pin both containers to the same core with --cpuset-cpus 0 as well. That option is covered in the next chapter.
Exercise
Three containers compete for one saturated core. Their weights are 1024, 1024, and 2048. What fraction of CPU time does the container with weight 2048 get, and what happens to its share if the other two containers go idle?
Show answer
The total weight is 1024 + 1024 + 2048 = 4096, so the 2048 container gets 2048 / 4096 = 50% of the contended CPU time. If the other two go idle, nothing competes with it, so it can use all of the core.