Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit useable host resources in Docker compose without swarm

I simply want to limit the resources of some Docker containers in a docker-compose file. The reason is simple: There are multiple apps/services running on the host. So I want to avoid, that a single container can use e.g. all memory, which harms the other containers.

From the docs I learned, that this can be done using resources. But this is beyond deploy. So I have to write my docker-compose file like the following example:

  php:     image: php:7-fpm     restart: always     volumes:       - ./www:/www     deploy:       resources:         limits:           memory: 512M 

This gave me the warning:

WARNING: Some services (php) use the 'deploy' key, which will be ignored. Compose does not support deploy configuration - use docker stack deploy to deploy to a swarm.

And that seems to be true: docker statsconfirms, the container is able to use all the ram from the host.

The documentation says:

Specify configuration related to the deployment and running of services. This only takes effect when deploying to a swarm with docker stack deploy, and is ignored by docker-compose up and docker-compose run.

But I don't need clustering. It seems that there is no other way to limit resources using a docker composer file. Why is it not possible to specify some kind of memorytag like the start-parameter in docker rundoes?

Example: docker run --memory=1g $imageName

This works perfectly for a single container. But I can't use this (at least without violating a clean separation of concerns), since I need to use two different containers.

Edit: Temp workaround

I found out, that I'm able to use mem_limit directly after downgrading from version 3 to version 2 (placing version: '2' on top). But we're currently on version 3.1, so this is not a long-time solution. And the docs say, that deploy.resources is the new replacement for v2 tags like mem_limit.

Someday, version 2 is deprecated. So resource management isn't possible any more with the latest versions, at least without having a swarm? Seems a worsening for me, can't belive this...

like image 400
Lion Avatar asked Feb 25 '17 15:02

Lion


People also ask

How do I restrict resources in docker?

To limit the maximum amount of memory usage for a container, add the --memory option to the docker run command. Alternatively, you can use the shortcut -m . Within the command, specify how much memory you want to dedicate to that specific container.

What can I use instead of docker Swarm?

Kubernetes is an open source container orchestration platform that was initially designed by Google to manage their containers. Kubernetes has a more complex cluster structure than Docker Swarm.

What is MEM limit in docker compose?

The MEM USAGE/LIMIT is at 2.133MiB out of the total 1.934GiB. Let's remove this container and start writing docker-compose scripts.


1 Answers

Since many Docker Compose users have complained about this incompatibility of compose v3 vs v2, the team has developed compatibility mode.

You can retain the same deploy structure that you provided and it will not be ignored, simply by adding the --compatibility flag to the docker-compose command (docker-compose --compatibility up), as explained here. I tested this with version 3.5 and verified with docker stats and can confirm that it works.

like image 110
Yonatan Wilkof Avatar answered Oct 18 '22 07:10

Yonatan Wilkof