How does docker use CPU cores from its host operating system?

Solution 1:

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";
}

Solution 2:

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/