48

My understading, based on the fact that Docker is based on LXC, is that Docker containers share various resources from its host operating system. My concern is with CPU cores. Here is a scenario:

  • a host linux OS has 8 cores
  • I have to deploy a set of docker containers on the host OS above.
  • Some of the docker containers that I need to deploy would be better suited to use 2 cores

a) So if I run all of the docker containers on that host, will they consume CPU/cores as needed like if they were being run as normal installed applications on that host OS ?

b) Will the docker container consume its own process and all of the processing that is contained in it will be stuck to that parent process's CPU core ?

c) How can I specify a docker container to use a number of cores ( 4 for example ). I saw there is a -C flag that can point to a core id, but it appears there is no option to specify the container to pick N cores at random.

gextra
  • 7,087
  • 7
  • 35
  • 57

4 Answers4

9

Currently, I don't think docker provides this level of granularity. It doesn't specify how many cores it allocates in its lxc.conf files, so you will get all cores for each docker, potentially (or possibly 1, I'm not 100% sure on that).

However, you could tweak the conf file generated for a given container and set something like

cpuset {
    cpuset.cpus="0-3";
}
Oliver Matthews
  • 6,755
  • 3
  • 27
  • 32
  • 38
    This doesn't really answer the question of "Will the application running in the container see and consume all 8 cores if it wants to?". However, I asked in IRC, and this is the case. If you have a multicore process running inside a single continer and run another multicore process in a different container, the resource contention will be handled by the linux kernel scheduler. – matt May 22 '14 at 16:59
  • 1
    where is located this file please – Montells Aug 22 '14 at 18:07
  • 1
    I think matt has the more accurate answer. – bvs Nov 22 '14 at 14:57
  • 13
    Note for Mac/Windows users: the CPUs available to docker are limited by the host machine running docker, which on Mac/Windows is the virtual machine running in the localhost OS. To make more cores available to a docker container, make sure the host VM has enough cores. Example: `docker-machine create --driver virtualbox --virtualbox-cpu-count 4 fourcpu`. Then, containers will have access to 4 CPUs, which is verifiable with `docker run nproc`, which should print `4`. – grisaitis Nov 29 '15 at 21:46
  • @grisaitis in Docker for Windows/Mac beta (not Docker Toolbox) the user can specify how many CPU cores and how much memory should be apportioned to Docker. – treeface Jul 19 '16 at 22:58
  • @treeface: This is because Docker for Windows/Mac uses a VM (https://docs.docker.com/docker-for-windows/) to build and run containers. So in that situation you constrain the "host" CPU which is not equivalent to the scenario in the question. – DragonTux Aug 09 '16 at 13:30
  • @DragonTux no, Docker for Windows/Mac Beta is not using a VM. It uses xhyve for virtualisation: https://docs.docker.com/engine/installation/mac/ – andreas Oct 01 '16 at 05:27
  • @DragonTux Refer to [Docker for Mac vs. Docker Toolbox](https://docs.docker.com/docker-for-mac/docker-toolbox/) for more information. [HyperKit](https://github.com/docker/HyperKit/) in Docker for Mac should be more lightweight than a real VM (and that's why they switched). – Franklin Yu Mar 27 '17 at 22:35
7

It might be that things changed in the latest (few) versions. Nowadays you can constrain your docker container with parameters for docker run: The equivalent for the current answer in the new docker version is docker run ubuntu /bin/echo 'Hello world --cpuset-cpus="0-3" However, this will limit the docker process to these CPU, but (please correct me if I am wrong) other containers could also request the same set. A possibly better way would be to use CPU shares.

For more information see https://docs.docker.com/engine/reference/run/

DragonTux
  • 673
  • 9
  • 17
4

From ORACLE documentation:

  To control a container's CPU usage, you can use the
  --cpu-period  and --cpu-quota options with the docker 
  create and docker run commands  from version 1.7.0 of Docker onward.

  The --cpu-quota option specifies the number of microseconds 
  that a container has access to CPU resources during a 
  period specified by --cpu-period.
  As the default value of --cpu-period is 100000, setting the 
  value of --cpu-quota to 25000 limits a container to 25% of 
  the CPU resources. By default, a container can use all available CPU resources, 
  which corresponds to a --cpu-quota value of -1.
Ijaz Ahmad Khan
  • 8,495
  • 5
  • 32
  • 59
2

So if I run all of the docker containers on that host, will they consume CPU/cores as needed like if they were being run as normal installed applications on that host OS?

Yes.

CPU

By default, each container’s access to the host machine’s CPU cycles is unlimited. You can set various constraints to limit a given container’s access to the host machine’s CPU cycles.


Will the docker container consume its own process and all of the processing that is contained in it will be stuck to that parent process's CPU core?

Nope.

Docker uses Completely Fair Scheduler for sharing CPU resources among containers. So containers have configurable access to CPU.


How can I specify a docker container to use a number of cores ( 4 for example ). I saw there is a -C flag that can point to a core id, but it appears there is no option to specify the container to pick N cores at random.

It is overconfigurable. There are more cpu options in Docker which you can combine.

--cpus= Specify how much of the available CPU resources a container can use. For instance, if the host machine has two CPUs and you set --cpus="1.5", the container is guaranteed at most one and a half of the CPUs.

--cpuset-cpus Limit the specific CPUs or cores a container can use. A comma-separated list or hyphen-separated range of CPUs a container can use, if you have more than one CPU. The first CPU is numbered 0. A valid value might be 0-3 (to use the first, second, third, and fourth CPU) or 1,3 (to use the second and fourth CPU).

And more...

Community
  • 1
  • 1
I159
  • 24,762
  • 27
  • 88
  • 124
  • It's also interesting to consider that one might be tempted to limit the CPU usgae of the system as a whole by configuring `CPUAffinity` for systemd in `/etc/systemd/system.conf` but it would appear that Docker completely ignores this and will use all the cores that it can. The only way to limit it is to use the aforementioned `--cpuset-cpus` option when creating or running a container. – starfry Jul 26 '19 at 13:12