55

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
Montells
  • 5,513
  • 3
  • 41
  • 48
  • 1
    yes, you can set mem_limit and cpu_shares http://docs.docker.com/compose/yml/#working95dir-entrypoint-user-hostname-domainname-mem95limit-privileged-restart-stdin95open-tty-cpu95shares – enrique-carbonell Mar 03 '15 at 18:23
  • 2
    `docker stats` allows to watch container resources. – ulidtko Nov 07 '17 at 14:18
  • Updated docs link: https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources use `mem_limit` in compose file. – levibostian Jan 24 '18 at 21:49

3 Answers3

62

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
jozala
  • 3,265
  • 1
  • 16
  • 13
  • 1
    I am using Docker 1.5. Apparently "docker stats" has been introduced in Docker 1.5: https://blog.docker.com/2015/02/docker-1-5-ipv6-support-read-only-containers-stats-named-dockerfiles-and-more/ – jozala Apr 07 '15 at 18:04
8

According to documentation, simple

mem_limit: 1000000000

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

mikemaccana
  • 81,787
  • 73
  • 317
  • 396
noisy
  • 5,701
  • 9
  • 47
  • 86
  • 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? – Montells Mar 03 '15 at 18:21
  • Link to compose docs talking about `mem_limit` and other options: https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources – levibostian Jan 24 '18 at 21:50
0

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.

enrique-carbonell
  • 4,574
  • 2
  • 25
  • 41
  • another simple way to test MEMORY limits is running code inside containers as follow: http://soichi.us/archives/11 but be careful with swap behavior as shown the reference of my answer. – enrique-carbonell Mar 04 '15 at 17:10