Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit memory usage in docker-compose?

Here is part of my docker-compose.yaml file

version: '3.4'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    working_dir: /app
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 23M

Starting it docker-compose up -d

When I do docker stats it says that limit is still 1.9GiB. What am I doing wrong?

CONTAINER ID      NAME     CPU %     MEM USAGE / LIMIT     MEM %
13b6588evc1e      app_1    1.86%     20.45MiB / 1.952GiB   1.02%
like image 262
John Glabb Avatar asked May 31 '19 17:05

John Glabb


2 Answers

deploy key only works in swarm mode and with docker-compose file version 3 and above.

In your case, use docker-compose file version 2 and define resource limits:

version: "2.2"

services:
  app:
    image: foo
    cpus: "0.5"
    mem_limit: 23m

See official docs here

like image 166
rajesh-nitc Avatar answered Oct 19 '22 23:10

rajesh-nitc


Are you running the docker-compose in swarm mode ? If not Recommended to run 2.x version of compose file format.

3.X require docker-compose to be run in swarm mode for new set of resource directives to take effect.

Alternatives in 2.X are cpu_shares, cpu_quota, cpuset, mem_limit, memswap_limit, mem_swappiness

like image 41
Shirine Avatar answered Oct 19 '22 23:10

Shirine