Can I use mem_limit in docker-compose? and How?

DockerFig

Docker Problem Overview


mem_limit is supported by docker-compose? How can I test it?

I have a following docker-compose.yml

repository:
  image: myregistry/my_nginx_image
  mem_limit: 60m
  volumes:
    - /etc/localtime:/etc/localtime
  ports:
    - "80:80"

How can I prove that the container actually does not exceed 60 mb of RAM?

I am using:

  • docker 1.3.1
  • docker-compose 1.1.0

Docker Solutions


Solution 1 - Docker

Yes. Memory limitation is supported by docker-compose and value can be set as in your example with "m" for megabytes.

It is possible to check what is the memory limit set for running Docker container using "docker stats" command.

If your container name is "repository_1" then use this command:

docker stats repository_1

The result of this will be simillar to this one:

CONTAINER       CPU %    MEM USAGE/LIMIT    MEM %     NET I/O
repository_1    1.93%    4.609 MiB/60 MiB   7.20%     1.832 KiB/648 B

Solution 2 - Docker

According to documentation, simple

mem_limit: 1000000000

should be enough. I guess, you should drop "m", and use bytes instead of megabytes.

Solution 3 - Docker

You can find how configure docker to limit resources (CPU & MEMORY) and how test your restrictions in this post written last year: resource-management-in-docker.

Solution 4 - Docker

If you use docker compose v3 instead of:

mem_limit: 60m

you need to specify memory limit in deploy section:

deploy:
  resources:
    limits:
      memory: 60m

This change is described here: https://docs.docker.com/compose/compose-file/compose-versioning/#version-2x-to-3x

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMontellsView Question on Stackoverflow
Solution 1 - DockerjozalaView Answer on Stackoverflow
Solution 2 - DockernoisyView Answer on Stackoverflow
Solution 3 - Dockerenrique-carbonellView Answer on Stackoverflow
Solution 4 - DockertigersoftView Answer on Stackoverflow