Can I use mem_limit in docker-compose? and How?
Asked Answered
K

4

62

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
Kimikokimitri answered 3/3, 2015 at 16:58 Comment(4)
yes, you can set mem_limit and cpu_shares docs.docker.com/compose/yml/…Deandre
docker stats allows to watch container resources.Musket
Updated docs link: docs.docker.com/compose/compose-file/compose-file-v2/… use mem_limit in compose file.Wert
Be careful, mem_limit is DEPRECATED : docs.docker.com/compose/compose-file/05-services/#mem_limitIlla
S
68

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
Shinleaf answered 2/4, 2015 at 21:26 Comment(2)
I am using Docker 1.5. Apparently "docker stats" has been introduced in Docker 1.5: blog.docker.com/2015/02/…Shinleaf
If you are using docker desktop on OSX you will also have to go into preferences and increase the memory limit above 2G or the mem_limit in docker is not honored.Nubile
M
10

According to documentation, simple

mem_limit: 1000000000

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

Macintosh answered 3/3, 2015 at 18:18 Comment(3)
Yes, I did that. Not getting any errors, but I'm not sure this delimiting the amount of memory to use. As I could check?Kimikokimitri
Link to compose docs talking about mem_limit and other options: docs.docker.com/compose/compose-file/compose-file-v2/…Wert
Be careful, mem_limit is DEPRECATED : docs.docker.com/compose/compose-file/05-services/#mem_limitIlla
K
4

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

Kelpie answered 23/12, 2021 at 16:48 Comment(1)
Yes, but please keep in mind the following note from the same linked document: "deploy configuration only takes effect when using docker stack deploy, and is ignored by docker-compose"Berkly
D
1

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.

Deandre answered 4/3, 2015 at 17:0 Comment(1)
another simple way to test MEMORY limits is running code inside containers as follow: soichi.us/archives/11 but be careful with swap behavior as shown the reference of my answer.Deandre

© 2022 - 2024 — McMap. All rights reserved.