'Resource limits specified in docker-compose.yml not taken into account by docker

I am trying to set resources limits in my docker-compose.yml file.

Here it is:

version: "3.7"
services:
  postgres:
    build: "docker/postgres"
    container_name: "postgres"
    ports:
    - 5432:5432
    environment:
      POSTGRES_USER: prodev
      POSTGRES_PASSWORD: prodev
      POSTGRES_MULTIPLE_DATABASES: pro_dev, pro_test
    networks:
    - my_proto_app
  the_api:
    deploy:
      resources:
        limits:
          cpus: '0.001'
          memory: 50M
        reservations:
          cpus: '0.0001'
          memory: 20M
    image: the_api:latest
    ports:
    - 8080:8080
    depends_on:
    - postgres
    links:
    - postgres
    networks:
    - my_proto_app
networks:
  my_proto_app:
    internal: false

However, when I run docker stats in order to gain insight into my resources limits, I notice that my limits are not taken into account:

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
c0b7d2fffc42        postgres            0.04%               20.89MiB / 1.952GiB   1.05%               148kB / 171kB       0B / 856kB          16
0a0f9e482f86        api_the_api_1       2.16%               739.5MiB / 1.952GiB   37.00%              409kB / 464kB       0B / 73.7kB         59

Can someone please help ?

edit: I run the app with the following command: docker-compose up



Solution 1:[1]

The deploy key in the docker-compose file doesn't work on docker-compose up (with compose file format version 3 and above). The deploy key which will be working only in swarm mode. To run it in swarm mode

  docker swarm init 

Sample docker-compose.yml to deploy in swarm deployments with CPU and Memory resource limits

  version: "3.3"
  services:
  tomcat:
    image: tomcat
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 250M
        reservations:
          cpus: '0.5'
          memory: 120M

Command to deploy in docker stack

  docker stack deploy --compose-file=docker-compose.yml stackname

Check the CPU and memory resouse limits using docker stats

Ref URL : https://docs.docker.com/compose/compose-file/compose-versioning/#version-2x-to-3x

If you want to set resource constraints on non swarm deployments, use Compose file format version 2.

Sample docker-compose.yml to deploy in non-swarm deployments with CPU and Memory resource limits

 version: "2.2"
 services:
   tomcat:
     image: tomcat
     cpus: "0.5"
     mem_limit: 512m

Run the docker-compose.yml file with command

  docker-compose up

Check the CPU and memory resouse limits using docker stats

Ref : https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources

Hope this helps !!!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yan Khonski