[英]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. 我只是想在docker-compose文件中限制某些Docker容器的资源。 The reason is simple: There are multiple apps/services running on the host. 原因很简单:主机上正在运行多个应用程序/服务。 So I want to avoid, that a single container can use eg all memory, which harms the other containers. 因此,我想避免一个容器可以使用例如所有内存,而这会损害其他容器。
From the docs I learned, that this can be done using resources
. 从我学到的文档中,可以使用resources
完成此操作。 But this is beyond deploy
. 但这超出了deploy
。 So I have to write my docker-compose file like the following example: 所以我必须像下面的例子一样写我的docker-compose文件:
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. 警告:某些服务(php)使用'deploy'键,该键将被忽略。 Compose does not support deploy configuration - use
docker stack deploy
to deploy to a swarm. Compose不支持部署配置-使用docker stack deploy
部署到集群。
And that seems to be true: docker stats
confirms, the container is able to use all the ram from the host. 这似乎是正确的: docker stats
确认,该容器能够使用主机中的所有ram。
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. 这仅在通过docker stack deploy部署到集群时才生效,并且被docker-compose up和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. 似乎没有其他方法可以使用docker composer文件来限制资源。 Why is it not possible to specify some kind of memory
tag like the start-parameter in docker run
does? 为什么无法像docker run
中那样指定某种memory
标签(如start-parameter)?
Example: docker run --memory=1g $imageName
示例: 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. 但是我不能使用它(至少不违反关注点的清晰区分),因为我需要使用两个不同的容器。
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). 我发现,从版本3降级到版本2后,我可以直接使用mem_limit
(将version: '2'
放在顶部)。 But we're currently on version 3.1, so this is not a long-time solution. 但是我们当前使用的是3.1版,因此这不是一个长期的解决方案。 And the docs say, that deploy.resources
is the new replacement for v2 tags like mem_limit
. 而且文档说, deploy.resources
是v2标签(例如mem_limit
)的新替代品。
Someday, version 2 is deprecated. 有一天,版本2已弃用。 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... 对我来说似乎更糟,不能相信这个...
Since many Docker Compose users have complained about this incompatibility of compose v3 vs v2, the team has developed compatibility mode . 由于许多Docker Compose用户抱怨 compose v3 vs v2的这种不兼容性,因此团队开发了兼容模式 。
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 . 你可以保持相同的部署结构,您提供的,它不会被忽略,简单地通过增加--compatibility标志泊坞窗,撰写命令(搬运工,撰写--compatibility起来),如解释在这里 。 I tested this with version 3.5 and verified with docker stats and can confirm that it works. 我使用3.5版进行了测试,并使用了docker stats进行了验证,可以确认它是否有效。
You can run the docker daemon in swarm mode on a single host. 您可以在单一主机上以集群模式运行docker守护程序。 It will add extra un-needed features like the etcd service discovery but that's all behind the scene. 它将添加额外的不需要的功能,例如etcd服务发现,但这一切都在幕后。
The Docker documentation has a "note" about it here https://docs.docker.com/engine/swarm/swarm-tutorial/#three-networked-host-machines Docker文档在此处对此有一个“注释” https://docs.docker.com/engine/swarm/swarm-tutorial/#three-networked-host-machines
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.