-->

Limit useable host resources in Docker compose wit

2020-08-09 06:55发布

问题:

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...

回答1:

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.



回答2:

You can run the docker daemon in swarm mode on a single host. It will add extra un-needed features like the etcd service discovery but that's all behind the scene.

The Docker documentation has a "note" about it here https://docs.docker.com/engine/swarm/swarm-tutorial/#three-networked-host-machines