linuxdockerdocker-composerhel7

How to specify CPU limit in docker compose version 2.4


I am using docker compose version 2.4 and I want to add addition setting in my .yml file

As I know we can limit the container to consume CPU ,

so I want to know what is the right syntax that is needed in order to specify the CPU limit in my docker container

As I understand the cpu limit for version 3 is like this

limits:
          cpus: '0.001'

but I not sure is also fit for 2.4 version ?

Here is example of my docker container file

more  docker.kafka-exporter.yml
---
version: '2.4'
services:
  kafka-exporter:
    mem_limit: "612m"
    image: kafka-exporter:v1.2.0
    restart: always
    network_mode: host
    container_name: kafka-exporter
    command: ["--kafka.server=kafka01.sys65.com:6667"]ump 
    ports:
      - 9308:9308
    logging:
      driver: "json-file"
      options:
        max-size: "15m"
        max-file: "1"

Note: the reason that we want to add CPU limit , is because a strange behavior that when docker container is up , then we are notices about high CPU load Avrg

links

https://www.baeldung.com/ops/docker-memory-limit

https://docs.docker.com/config/containers/resource_constraints/#configure-the-default-cfs-scheduler

https://www.hezser.de/blog/2020/12/27/docker-compose-vs-cpu-limit/

https://docs.scylladb.com/operating-scylla/procedures/tips/best-practices-scylla-on-docker/


Solution

  • As explained at https://docs.docker.com/compose/compose-file/compose-file-v2/ the cpus is available since versione 2.2:

    Official guide screenshot