Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 524
balteo Avatar asked Sep 13 '18 07:09

balteo


People also ask

How do I limit docker total resources?

To limit the maximum amount of memory usage for a container, add the --memory option to the docker run command. Alternatively, you can use the shortcut -m . Within the command, specify how much memory you want to dedicate to that specific container.

What is MEM limit in Docker compose?

The MEM USAGE/LIMIT is at 2.133MiB out of the total 1.934GiB.

What is the maximum amount of RAM a container can consume if the memory flag is not used?

Limiting Memory This sets a hard limit. That means that under no circumstances will the container be allowed to use more than 256 MB of RAM.

How do I fix OOM issue in a docker container?

By default, if an out-of-memory (OOM) error occurs, the kernel kills processes in a container. To change this behavior, use the --oom-kill-disable option. Only disable the OOM killer on containers where you have also set the -m/--memory option.


1 Answers

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 !!!

like image 192
Prem Avatar answered Oct 21 '22 08:10

Prem