brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker run --cpus caps how much CPU a container can use

Docker
~/tips/tip-docker-run-cpus

docker run -d --name encoder --cpus="1.5" example/app:1.0

Sets a hard ceiling on the CPU a container may use, in whole or fractional cores.

One runaway container can make a small home server feel dead. I would rather give the greedy ones a ceiling, and --cpus is the readable way to do it.

The flag takes a number of CPUs, fractions allowed. The resource constraints page uses a two-CPU host as its example: --cpus="1.5" means the container can use at most one and a half CPUs. It is shorthand for --cpu-period=100000 --cpu-quota=150000, so you never have to do that math yourself.

docker run -d --name encoder --cpus="1.5" example/app:1.0

You can also change it on a container that already exists, without recreating it:

docker update --cpus="0.5" encoder

In Compose the matching service attribute is cpus: 1.5, and the reference says 0.000 means no limit.

--cpus is a hard ceiling, so the container is throttled even when the rest of the machine is idle. If you only want to give some containers priority when the CPU is busy, --cpu-shares is the soft version: a relative weight with a default of 1024 that only matters when there is contention. I use the ceiling for jobs that should never hog the machine and shares for the rest.

Check the effect with docker stats --no-stream while the container is busy.

Further reading: Resource constraints, docker run reference

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link