I have a 4-core CPU, I want to allocate 50% CPU resource to a docker container.
After reading the docker-run
manual and config.go
source code.
I still don't know how to use the -c, --cpu-shares=0
option.
docker run -c 0.5 -i -t ubuntu /bin/bash
Or
docker run -c 2 -i -t ubuntu /bin/bash
Note: PR 15078 is implementing (Dec. 2015) support for changing resources (including CPU) both for stopped and running container (possibly docker 1.10 ou 1.11)
Note that making changes via
docker set
should persist.I.e., those changes would be permanent (updated in the container's JSON)
cpu-shares is a 'relative weight', relative to the default setting of 1024, so if you had two containers running on the same core, you could give them the CPU 50-50 or 80-20 or whatever you wanted by adjusting the numbers. It is an integer.
You cannot give an overall limit, as you want to, using this flag, but you can restrict the set of CPUs that the container runs on using
--cpuset
mentioned here.The number 1024 is in the Cgroups docs.
This blog post from Marek Goldmann explains resource management in Docker.
See also Setting absolute limits on CPU for Docker containers, which says it can be done with lxc (older Docker implementation) but not libcontainer (current Docker implementation).
It depends on the environment, so there is no straight answer but keep reading.
From the
docker run --help
command:Since Docker is based on cgroups. The CPU will be distributed among the running containers. By default the value is
1024
.So, if we have 2 containers, one for the database and one more for the web server
Will give 60% to the
db
container (614 is 60% of 1024) and 40% to theweb
container.For further reading see:
cpuset
option:--cpuset="" CPUs in which to allow execution (0-3, 0,1)
Take a look here, this is apparently what you were looking for:
https://docs.docker.com/engine/reference/run/#cpu-period-constraint
The default CPU CFS (Completely Fair Scheduler) period is 100ms. We can use --cpu-period to set the period of CPUs to limit the container’s CPU usage. And usually --cpu-period should work with --cpu-quota.
Examples:
If there is 1 CPU, this means the container can get 50% CPU worth of run-time every 50ms.
period and quota definition:
Within each given "period" (microseconds), a group is allowed to consume only up to "quota" microseconds of CPU time. When the CPU bandwidth consumption of a group exceeds this limit (for that period), the tasks belonging to its hierarchy will be throttled and are not allowed to run again until the next period.
Since Docker 1.13, in your 4-core machine just add
docker container run --cpus 2.0 [args...]
.Explanation from this blog post:
Also check the docs.