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?
2 Answers
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:
-
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? 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.– BMitchApr 3, 2019 at 16:20
-
-
2
-
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?– MagnusOct 26, 2021 at 18:00
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
--cpuset-cpus
if you wanted to change that.