简体   繁体   English

我可以限制每个Docker容器使用的内存和cpu吗?

[英]Can I limit memory & cpu used per docker container?

I was wondering if there's a way to limit or cap the max memory & processing power used by docker containers on host level. 我想知道是否有办法限制或限制主机级别上的Docker容器使用的最大内存和处理能力。 I'm under the impression that the memory options only add provided memory at container launch, but do not limit it permanently. 我的印象是,内存选项仅在容器启动时添加提供的内存,而不是永久地限制它。

As I use containers with dotnet apps (hosted dlls), mono'd/wine'd exes, java apps & linux deamons, it would be great if I could make sure that, for instance, continues java memory allocation would not make other containers crash. 当我将容器与dotnet应用程序(托管的dll),单/葡萄酒exe,Java应用程序和linux恶魔一起使用时,如果能够确保例如继续进行Java内存分配不会使其他容器成为容器,那将是非常不错的崩溃。 I'm using java memory options (xms, xmx) for these containers now, and am doing similar to the other containers. 我现在为这些容器使用java内存选项(xms,xmx),并且与其他容器类似。 I'm lacking a docker based system to manage and monitor cpu & memory. 我缺少基于docker的系统来管理和监视CPU和内存。 Also, percentile scaling for memory & cpu would be useful, allowing one container to grab a lot of processing power & memory at peak moments. 同样,内存和cpu的百分位数缩放将很有用,允许一个容器在高峰时刻获得大量处理能力和内存。 I can monitor this on host process level, not on docker level. 我可以在主机进程级别而不是在docker级别上监视它。

And I'm not even talking about a multi-host setup (swarm, rancher, kubernetes) yet. 而且,我什至没有在谈论多主机设置(swarm,rancher,kubernetes)。 Or is my question to general? 还是我的问题是一般性的?

The docker run command includes options to limit memory and cpu , in particular the -c and -m options. docker run命令包含用于限制内存和cpu的选项 ,尤其是-c-m选项。

With a compose file and swarm mode (which can be a single node swarm cluster), these are configured with a resources section per service : 通过组合文件和群集模式(可以是单节点群集集群),这些服务通过每个服务资源部分进行配置:

version: '3'
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 50M
        reservations:
          cpus: '0.25'
          memory: 20M

For docker-compose support, you can use the version 2 syntax for a similar result: 对于docker-compose支持,可以使用版本2语法获得类似结果:

version: '2.2'
services:
 test:
    image: busybox:latest
    command: tail -f /dev/null

    cpus: '0.50'
    mem_limit: '50M'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM