Docker Community Forums

Share and learn in the Docker community.

Docker Compose Yaml File Not Setting Resource Limits

I have a docker-compose.yml file that I call to deploy my container and it seems to work fine however when I look at the container statistics, I can’t see my resource limits being honored for CPU / RAM.

Here are the container stats from running “docker-compose up -d”:

CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O        BLOCK I/O       PIDS
ee5bbad3c458   db1       0.02%     126.2MiB / 15.55GiB   0.79%     5.61MB / 7MB   184MB / 357MB   8

Below is my docker compose yaml file:

# docker-compose.yml
version: '3'
services:
  database:
    image: postgres # use latest official postgres version
    restart: always
    container_name: db1
    ports: 
      - 5432:5432
    deploy:
        resources:
            limits:
              cpus: 2
              memory: 2G 
    volumes:
      - /mnt/docker/postgresql/db1/pgdata:/var/lib/postgresql/data/

I am not getting any errors when I run

docker-compose up -d

The container is running fine but appears to not honor the resource limits. Appreciate any help on why…

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 : Compose file versions and upgrading | Docker Documentation

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 : Compose file version 2 reference | Docker Documentation

Hope this helps !!!

I am simply trying to deploy a container using docker-compose with 2 gb of ram and 2 full cpu cores.
I am not using docker swarm or anything else. I simply want to use a docker-compose YAML file to avoid having to issue “docker run”

For example:

docker run \
 --restart unless-stopped \
 -d \
 -m 2g \
 --cpus=2 \
 --name=db1 \
 -p 3306:3306 \
 -v /mnt/docker/mysql/mysql-db1/:/var/lib/mysql \
 -e MYSQL_ROOT_PASSWORD=..xxxxxxxxxxxx \
 mysql:latest

I can not find an example of how this is done without confusing complexity or the example referencing the deploy key which appears to only be honored by swarm which I have no use for.