I’m using docker build -t my-image .
on a VM with 4 CPU cores.
I would like to use only 2 of them to build the image, because during the build all four cores go to 100%.
Is it possible?
I’m using docker build -t my-image .
on a VM with 4 CPU cores.
I would like to use only 2 of them to build the image, because during the build all four cores go to 100%.
Is it possible?
2
Answers
I assume that the VM is based on a linux distribution, so the solution should be:
You can limit the concurrency in buildx which can be a poor substitute for limiting the CPU utilization:
This would applies to builds performed with
docker buildx build
. Note that if individual steps use multiple CPUs, then this will not stop those steps from using all of the CPU, which is why I consider this a poor substitute.If you were to use the kubernetes driver, there is a
limits.cpu=2
which should work better.Since docker is using
/docker/buildx
as the parent cgroup for all buildx containers by default, you could attempt to set the limit there. However that change will vary based on your cgroup version, and won’t be a good option if you are in Docker Desktop since that uses an embedded VM.If you are in Docker Desktop, you can limit the CPU usage of the entire embedded VM from the Docker Desktop settings. That will also limit any running containers, not just builds.
Reference:
buildx create
: https://docs.docker.com/engine/reference/commandline/buildx_create/buildkitd.toml
: https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md