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.
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
--cpusquota applies on an idle machine too: encoder with--cpus 0.5stayed 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-streamis a usage snapshot: CPU share, memory and its ceiling for each container. -
Without
--memorythe ceiling is all of the machine's memory, and a shortage will hit any process on it. -
--cpuslimits the share of CPU time: the process runs slower and keeps running.