限制Docker组合中可用的主机资源而无群 [英] Limit useable host resources in Docker compose without swarm

查看:96
本文介绍了限制Docker组合中可用的主机资源而无群的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在docker-compose文件中限制某些Docker容器的资源.原因很简单:主机上正在运行多个应用程序/服务.所以我想避免单个容器可以使用例如所有内存,这会损害其他容器.

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.

根据我学到的文档,可以使用resources完成此操作.但是,这超出了deploy的范围.因此,我必须像以下示例一样编写我的docker-compose文件:

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

这给了我警告:

警告:某些服务(php)使用'deploy'键,该键将被忽略. Compose不支持部署配置-使用docker stack deploy部署到集群.

这似乎是真的:docker stats确认,容器能够使用主机中的所有内存.

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

文档说:

指定与服务的部署和运行有关的配置.这仅在通过docker stack deploy部署到集群时才生效,而被docker-compose up和docker-compose run忽略.

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 composer文件来限制资源.为什么不能像docker run中的开始参数那样指定某种memory标记?

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?

示例: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.

我发现,从版本3降级到版本2(将version: '2'放在顶部)后,我可以直接使用mem_limit.但是我们当前使用的是3.1版,因此这不是一个长期的解决方案.而且文档说,deploy.resources是v2标签(例如mem_limit)的新替代品.

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.

某天,版本2已弃用.因此,至少在没有大量资源的情况下,使用最新版本就不可能进行资源管理了吗?对我来说似乎更糟,不能相信这个……

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

推荐答案

由于许多Docker Compose用户具有抱怨关于Compose v3与v2的这种不兼容性,团队开发了兼容模式.

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

您可以保留与您提供的相同的部署结构,只需将--compatibility标志添加到docker-compose命令(docker-compose --compatibility up),就可以将其忽略,如此处.我使用3.5版进行了测试,并使用了docker stats进行了验证,可以确认它是否有效.

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.

这篇关于限制Docker组合中可用的主机资源而无群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆