I am trying to set hard CPU and memory limits in a Docker Swarm consisting of three VMs. I am using the cpu and memory limit configurations suggested by docker documentation in my docker-compose.yml file. My docker-compose.yml file looks like
version: "3"
services:
app:
# replace username/repo:tag with your name and image details
image: hifzak/testing:part2
deploy:
replicas: 10
resources:
limits:
cpus: "0.5"
memory: 4M
restart_policy:
condition: on-failure
The CPU and memory resources of my host machine and the VMs are shown in the figure below. My host machine has 4 CPUs and all the VMs have 1 CPU each.
To figure out if Docker containers can limit their resources, I am running a test program with an infinite loop in my swarm. One of the snapshots from my experiments is shown below. It shows docker stats results on the three VMs (VM1: bottom left, VM2: top right, VM3: bottom right).
Looking at the results, I have a few questions.
- How is the CPU limited to
50%for each container? - Each VM has
1CPU then how come on one VM, docker containers CPU% sum exceeds100%? - In the image above, I have
7docker containers running and sum of CPU% for all of them is22+21+88+52+66+78+76 = 403approx. which
means that the swarm is using4cores instead of3. Is it
possible that docker allows the swarm to use CPU resources of the
host machine as well if needed?
Can anybody please guide me with these questions? Thank you.

