Introduction#

  • understand memory
  • understand cpu-shares
  • understand cpu-period and cpu-quota
  • pressure test

Hello Container#

docker container run --name hello IMAGE /bin/sh -c "while:; do echo 1; done;"

CPU Request and Limits#

experiment 1: understand cpu-shares

  • cpu-shares set the relative cpu usage between containers
  • it is enforced only when CPU cycles are constrained
  • when plenty of CPU cycles, all containers use as much CPU as they need

get imageId

sudo docker images

run some containers

sudo docker run -p 80:8080 --cpu-shares 1024 --name cpu-1 $imageId
sudo docker run -p 81:8080 --cpu-shares 2048 --name cpu-2 $imageId

then run the pressure test, it is epxected that

  • cpu-2 uses CPU twice time cpu-1
  • however, this only happen when there is CPU cycles constrained
  • so, normaly we still see they use the same CPU usage

experiment 2: understand cpu-quota and cpu-period

sudo docker run -p 80:8080 --cpu-shares 1024 --cpu-period=100000 --cpu-quota=1000 --name cpu-1 $imageId
sudo docker run -p 81:8080 --cpu-shares 2048 --cpu-period=100000 --cpu-quota=2000 --name cpu-2 $imageId
sudo docker run -p 81:8080 --cpu-shares 2048 --cpu-period=100000 --cpu-quota=10000 --name cpu-3 $imageId

then do the pressure test

  • at most, cpu-1 can use 1 percent of CPU cycles
  • at most, cpu-2 can use 2 percent of CPU cycles
  • at most, cpu-3 can use 10 percent of CPU cycles

Pressure Test#

while true
do
# curl -s http://127.0.0.1:80 > /dev/null
curl -s http://127.0.0.1:81 > /dev/null
# curl -s http://127.0.0.1:82 > /dev/null
done;

Reference#

  • cpu-shares

  • cpu-shares blog

  • docker cpu limit