skip to Main Content

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


  1. I assume that the VM is based on a linux distribution, so the solution should be:

    taskset --cpu-list 0,1 docker build -t my-image .
    
    Login or Signup to reply.
  2. You can limit the concurrency in buildx which can be a poor substitute for limiting the CPU utilization:

    $ cat buildkitd.toml
    [worker.oci]
      max-parallelism = 2
    
    $ docker buildx create --config buildkitd.toml --driver docker-container --use limited
    

    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:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search