Linux Labs

Containers: Docker and compose / Stage 10 of 11

Limits, OOMKilled and the user in a container

Not running Starting usually ~20 s
Leave the lesson

Step 4 of 9 · theory · 4 min

The CPU quota

encoder spins an endless loop, and yet its share stays around 50%. That is the work of the --cpus option.

--cpus 0.5 is a CPU time quota: for every second, the container's processes get no more than half a second of one core's work. Once the share is used up, the process waits for the next slice. The lesson machine has one core, so encoder stays at about 50%. docker inspect keeps the quota in billionths of a core: .HostConfig.NanoCpus for encoder is 500000000.

CPU can be given to a process later, but memory it has already taken cannot be taken back without breaking its work. That is why a process is ended for overusing memory, and made to wait for overusing the CPU.

Check yourself 1 question

Video encoding in a container with --cpus 0.25 would take a whole core. What will docker ps and docker stats show a minute later?

  • The kernel does not kill for CPU: the process waits for its share and runs slower. It ends processes for overusing memory.

  • The --cpus quota applies on an idle machine too: encoder with --cpus 0.5 stayed at about 50%, although the processor had nothing else to do.

The --cpus quota limits the share of CPU time: the process uses it up and waits for the next slice. The container keeps running, and docker stats shows about 25%.

Key points

  • docker stats --no-stream is a usage snapshot: CPU share, memory and its ceiling for each container.
  • Without --memory the ceiling is all of the machine's memory, and a shortage will hit any process on it.
  • --cpus limits the share of CPU time: the process runs slower and keeps running.
Ubuntu 24.04
Loading…