简体   繁体   English

限制Docker组合中可用的主机资源而无群

[英]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. 但是我不能使用它(至少不违反关注点的清晰区分),因为我需要使用两个不同的容器。

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). 我发现,从版本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.

相关问题 如何在不删除的情况下以群模式停止/启动 docker 容器? - How to stop/start docker containers in swarm mode without removing? 如何使用 docker-compose 使用主机网络配置 - how to use host network config using docker-compose 写入主机操作系统的目录 - 通过 docker-compose 文件传递指令 - Writing to the directory of Host OS - Passing the instruction with docker-compose file 什么是 Compose 文件中的 --add-host=host.docker.internal:host-gateway 的等价物 - What is the equivalent of --add-host=host.docker.internal:host-gateway in a Compose file Docker(撰写):没有 sudo 的连接被拒绝 - Docker (compose): connection refused without sudo 如何在 docker compose 2.4 版中指定 CPU 限制 - How to specify CPU limit in docker compose version 2.4 Docker-compose 单个主机上的多个实例 - Docker-compose multiple instances on a single host machine 在docker中使用覆盖使用多主机网络功能需要swarm - Is swarm required for using multi-host networking feature using overlay in docker Docker Swarm 上的卷不是持久的? - Volume on Docker Swarm is not persistent? docker 容器的用户权限是否会影响主机对 docker-compose.yml 中挂载卷的权限? - Can user privilege of docker container impact permission of host machine on mounted volume in docker-compose.yml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM