6

Where exactly and how to put in docker-compose.yml file the maximum limit of memory that a docker will use?

version: "3"
services:
  mysql:
    image: "mysql:latest"
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: test
  backend:
    image: "backend:latest"
    ports:
      - 19001:19001
      - 80:80
      - "9001:9000"
    environment:
      DB_USERNAME: test
      DB_PASSWORD: test
      DB_URL: jdbc:mysql://mysql:3306/test
      jdbc_url: jdbc:mysql://mysql:3306
      JAVA_HOME: "/application/jdk8"
    links:
      - "mysql:mysql"

Currently I am running a backend app and it is running out of memory (crashing with memory exception) - this is usage just before the crash:

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
71f94ad4b16c        docker_backend_1    187.58%             1.113GiB / 1.952GiB   57.01%              92.4MB / 61.2MB     147MB / 180MB       73
dc2ec6cca410        docker_web_1        0.38%               95.71MiB / 1.952GiB   4.79%               2.05kB / 0B         59.6MB / 41kB       21
0960ca70127a        docker_mysql_1      0.04%               277.3MiB / 1.952GiB   13.87%              60.2MB / 64.3MB     62.8MB / 3.58GB     51
Joe
  • 11,983
  • 31
  • 109
  • 183
  • 1
    https://stackoverflow.com/questions/44533319/how-to-assign-more-memory-to-docker-container – Robert Mar 30 '18 at 07:53
  • Thank you! It was about docker global setup that was limiting to 2GB.. I set it to 6GB in global settings, restarted docker and now is not crashing... – Joe Mar 30 '18 at 11:32
  • Nice. I supposed that thanks to the output you posted. Note that the answer there is mine too :) – Robert Mar 30 '18 at 12:12
  • I documented the answer here for future searchers. – Robert Mar 30 '18 at 13:18

2 Answers2

4

Seeing the 1.952GiB limit in the docker stats output, I can guess that the problem is the default configuration that the docker machine has: it is assigned with 2GB of memory by default.

As per in my other answer here, you can see how to configure docker to allow more memory for containers.

Robert
  • 33,429
  • 8
  • 90
  • 94
3

Add a deploy.resources section to the service you want to restrain. For example:

version: "3"
services:
  mysql:
    image: "mysql:latest"
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 500M
        reservations:
          cpus: '0.25'
          memory: 200M

See the docs: https://docs.docker.com/compose/compose-file/#resources

Yuankun
  • 6,875
  • 3
  • 32
  • 34
  • 2
    This only works if you're using docker swarm. If you use docker-compose format >= 2.1, you can set these values for non-swarm deployments: https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources – testworks Jan 31 '20 at 04:51