Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing cpu-shares and cpuset-cpus in Docker

I would like to run two containers with the following resource allocation:

  • Container "C1": reserved cpu1, shared cpu2 with 20 cpu-shares
  • Container "C2": reserved cpu3, shared cpu2 with 80 cpu-shares

If I run the two containers in this way:

docker run -d --name='C1' --cpu-shares=20 --cpuset-cpus="1,2" progrium/stress --cpu 2

docker run -d --name='C2' --cpu-shares=80 --cpuset-cpus="2,3" progrium/stress --cpu 2

I got that C1 takes 100% of cpu1 as expected but 50% of cpu2 (instead of 20%), C2 takes 100% of cpu3 as expected and 50% of cpu2 (instead of 80%).

It looks like the --cpu-shares option is ignored. Is there a way to obtain the behavior I'm looking for?

like image 442
Giovanni Quattrocchi Avatar asked Nov 09 '22 22:11

Giovanni Quattrocchi


1 Answers

docker run mentions that parameter as:

--cpu-shares=0                CPU shares (relative weight)

And contrib/completion/zsh/_docker#L452 includes:

"($help)--cpu-shares=[CPU shares (relative weight)]:CPU shares:(0 10 100 200 500 800 1000)"

So those values are not %-based.

The OP mentions --cpu-shares=20/80 works with the following Cpuset constraints:

 docker run -ti --cpuset-cpus="0,1" C1 # instead of 1,2
 docker run -ti --cpuset-cpus="3,4" C2 # instead of 2,3

(those values are validated/checked only since docker 1.9.1 with PR 16159)

Note: there is also CPU quota constraint:

The --cpu-quota flag limits the container’s CPU usage. The default 0 value allows the container to take 100% of a CPU resource (1 CPU).

like image 120
VonC Avatar answered Nov 15 '22 07:11

VonC