181

Lets say I am running a multiprocessing service inside a docker container spawning multiple processes, would docker use all/multiple cores/CPUs of the host or just one?

3
  • 14
    As many CPUs as the cgroup it's in is allowed to use. Which, by default, isn't limited. See --cpuset-cpus if you wanted to change that. Jun 17, 2016 at 1:01
  • 3
    (A docker container is just a bunch of private namespaces -- since it isn't emulating CPUs, it would need to go out of its way to impose any restrictions on them; Linux kernels provide facilities to allow such going-out-of-one's-way, but that's still something that needs to actually be explicitly performed). Jun 17, 2016 at 1:03
  • How about Docker Toolbox on Windows, which uses VirtualBox?
    – Egor Kraev
    Apr 20, 2017 at 9:33

2 Answers 2

162

As Charles mentions, by default all can be used, or you can limit it per container using the --cpuset-cpus parameter.

docker run --cpuset-cpus="0-2" myapp:latest

That would restrict the container to 3 CPU's (0, 1, and 2). See the docker run docs for more details.


The preferred way to limit CPU usage of containers is with a fractional limit on CPUs:

docker run --cpus 2.5 myapp:latest

That would limit your container to 2.5 cores on the host.


Lastly, if you run docker inside of a VM, including Docker for Mac, Docker for Windows, and docker-machine, those VM's will have a CPU limit separate from your laptop itself. Docker runs inside of that VM and will use all the resources given to the VM itself. E.g. with Docker for Mac you have the following menu:

Docker for Mac Advanced settings

7
  • Would two docker containers running on the same VM with 2 CPUs affect performance if they are both spawning processes? So both spawns two processes for a total of four when there is only 2 CPUs on the VM?
    – cocoPuffs
    Apr 3, 2019 at 14:48
  • 1
    @cocoPuffs unless you specify CPU limits, it's the same as if you run those processes outside of a container on the same host. Adding CPU limits to each container can prevent processes in one container from using all of the CPU resources for itself.
    – BMitch
    Apr 3, 2019 at 16:20
  • can you run multiple containers on single cpu? Oct 24, 2020 at 15:41
  • 2
    @MuhammadUmer yes, same as running multiple processes on one CPU.
    – BMitch
    Oct 25, 2020 at 0:01
  • BTW, why is the "preferred" way to limit CPU usage of containers with a "fractional" limit? Are you just demonstrating that you can use fractions, or is there actually an advantage to them over integer CPU counts?
    – Magnus
    Oct 26, 2021 at 18:00
17

Maybe your host VM has only one core by default. Therefore you should increase your VM cpu-count first and then use --cpuset-cpus option to increase your docker cores. You can remove docker default VM using the following command then you can create another VM with optional cpu-count and memory size.:

docker-machine rm default
docker-machine create -d virtualbox --virtualbox-cpu-count=8 --virtualbox-memory=4096 --virtualbox-disk-size=50000 default

After this step you can specify number of cores before running your image. this command will use 4 cores of total 8 cores.

docker run -it --cpuset-cpus="0-3" your_image_name

Then you can check number of available core in your image using this command:

nproc

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.