Sub topic 11
Checking that limits are applied
This chapter assumes you can run containers with docker run, and that you have already set memory and CPU limits as covered in earlier chapters. Setting a flag does not prove it took effect. Two commands let you check: docker inspect shows the limits Docker recorded, and docker stats shows live usage against those limits.
Reading configured limits with docker inspect
docker inspect prints the container's configuration as JSON. The limits live under HostConfig. Start a container with limits, then read them back.
docker run -d --name web --memory 256m --cpus 0.5 nginx:alpine
docker inspect --format 'mem={{.HostConfig.Memory}} nanocpus={{.HostConfig.NanoCpus}}' webTypical output:
mem=268435456 nanocpus=500000000Docker stores values in base units:
| Field | Unit | Example meaning |
|---|---|---|
| `HostConfig.Memory` | bytes | 268435456 is 256 MiB |
| `HostConfig.MemorySwap` | bytes | total of memory plus swap |
| `HostConfig.MemoryReservation` | bytes | the soft limit |
| `HostConfig.NanoCpus` | billionths of a core | 500000000 is 0.5 cores |
| `HostConfig.CpuShares` | relative weight | 1024 is the default |
| `HostConfig.CpusetCpus` | core list | `0,1` means cores 0 and 1 |
A value of 0 means no limit is set. If you expected a limit and see 0, the flag was not applied. That often happens when a value was passed to the wrong container or a Compose file was not read the way you thought.
Watching live usage with docker stats
docker stats streams CPU and memory use for running containers. Add --no-stream to print one snapshot and exit.
docker stats --no-stream webTypical output:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
3f1c9a2b7d10 web 0.00% 4.5MiB / 256MiB 1.76% 976B / 0B 0B / 0B 5Read the MEM USAGE / LIMIT column first. The right side shows the limit that is in effect. If it shows your host's total RAM instead of your value, the container has no memory limit.
The CPU % column works differently. It is measured against the whole host, where one fully busy core counts as 100%. A container limited to --cpus 0.5 therefore tops out near 50%, not 100%.
Confirming that the limit has an effect
Configuration and usage tell you what is set. To see enforcement, create load and watch the numbers.
docker run -d --name burn --cpus 0.5 alpine sh -c 'while :; do :; done'
sleep 5
docker stats --no-stream burn
docker rm -f burnThe loop would use a full core without a limit. With --cpus 0.5, CPU % should stay close to 50%. The result confirms that the quota is being enforced, not only recorded.
Common problems
- Limit shows as 0 in inspect. The flag was not applied. Check the exact command or Compose file.
- Stats shows host memory as the limit. No memory limit exists for that container.
- CPU % looks too high. A container using two cores shows near 200%. Compare against
--cpus, not against 100%. - Warning about swap or cgroup support. Some hosts disable memory or swap accounting. Docker prints a warning at
docker run, and the limit may not be enforced as expected.
Exercise
You start a container with --cpus 1.5. What value should docker inspect report for HostConfig.NanoCpus, and what CPU % ceiling should docker stats show if the container runs a loop on every available core?
Show answer
NanoCpus should be 1500000000, because it counts billionths of a core. The CPU % should stay near 150%, since 100% equals one fully used core on the host.