skip to Main Content

I want to set a CPU limit on a container in a docker-compose file.

When I execute the following:

docker run -d --rm --cpus 0.3 --name aaa progrium/stress -c 8 -t 20s

The container is taking 30% of a core according to docker stats:

CONTAINER ID   NAME                                               CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
d6be318a85e6   aaa                                                29.01%    1.328MiB / 3.805GiB   0.03%     806B / 0B         0B / 0B           9

And the limit is set according to docker inspect:

$ docker inspect aaa | grep -i cpu
            "CpuShares": 0,
            "NanoCpus": 300000000,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "CpuCount": 0,
            "CpuPercent": 0,

This is expected behavior. But when I write the following docker-compose.yml file:

version: '3'
services:
  aaa:
    image: progrium/stress
    command: -c 8 -t 20s
    deploy:
      resources:
        limits:
          memory: 100M
          cpus: '0.3'

And start the project with docker-compose up -d ; I got the following stats output:

CONTAINER ID   NAME                                               CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
a94118f4a5f7   docker-aaa-1                                       87.74%    1.359MiB / 100MiB     1.36%     806B / 0B         1.91MB / 0B       9

And the limit is not set in the inspect output. I expected to see a CPU limit:

$ docker inspect docker-aaa-1 | grep -i cpu
            "CpuShares": 0,
            "NanoCpus": 0,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "CpuCount": 0,
            "CpuPercent": 0,

My conclusions are:

  • My docker engine is able to set cpu limits
  • The limits in the docker-compose.yml file are read (docker stats explicitly show memory limit
  • docker-compose fails to set limit

I tried to set cpu limit the old way (as mentioned in the compose file documentation).

version: '2'
services:
  aaa:
    image: progrium/stress
    command: -c 8 -t 20s
    cpus: 0.3

The result is the same (no limit on CPU). And no error message from docker-compose.

CONTAINER ID   NAME                                               CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
8658bc78c795   docker-aaa-1                                       180.75%   1.367MiB / 3.805GiB   0.04%     1.18kB / 0B       0B / 0B           9

How can I ask docker-compose to set the CPU limit? What documentation or configuration lines did I miss?

Thanks for your help!

Versions:

$ docker-compose -v
Docker Compose version v2.4.1
$ docker -v
Docker version 20.10.21, build baeda1f
$ uname -a
Linux hostname 5.10.0-18-amd64 #1 SMP Debian 5.10.140-1 (2022-09-02) x86_64 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 11 (bullseye)
Release:        11
Codename:       bullseye

2

Answers


  1. Chosen as BEST ANSWER

    Turns out this is a solved bug, an update fixed it… Thanks for your time.


  2. I just looked into it and it seems like this is an issue regarding docker swarm mode and the normal mode. The v3 format was developed specifically for docker swarm and the deploy key only works for docker in swarm mode. As far as I can see there are 3 solutions to your problem:

    1. Initialize a swarm cluster on your local machine (docker swarm init)
    2. Use docker-compose v2 for everything (version: '2')
    3. Use docker-compose in compatability mode and hope that all your needed options are translated correctly (--compatibility)

    See the discussion about this here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search