Setting Memory And CPU Limits In Docker – 在Docker中设置内存和CPU限制

最后修改: 2020年 11月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

There are many cases in which we need to limit the usage of resources on the docker host machine.

有很多情况下,我们需要限制docker主机上的资源使用。

In this tutorial, we’ll learn how to set the memory and CPU limit for docker containers.

在本教程中,我们将学习如何为docker容器设置内存和CPU限制。

2. Setting Resources Limit With docker run

2.用docker run设置资源限制

We can set the resource limits directly using the docker run command. It’s a simple solution. However, the limit will apply only to one specific execution of the image.

我们可以直接使用docker run命令设置资源限制。这是一个简单的解决方案。然而,该限制将只适用于镜像的一个特定执行。

2.1. Memory

2.1.记忆

For instance, let’s limit the memory that the container can use to 512 megabytes.

例如,让我们把容器可以使用的内存限制在512兆字节。

To constrain memory, we need to use the m parameter:

为了约束内存,我们需要使用m参数

$ docker run -m 512m nginx

We can also set a soft limit called a reservation.

我们还可以设置一个软限制,称为保留。

It’s activated when docker detects low memory on the host machine:

当docker检测到主机上的内存不足时,它就会被激活。

$ docker run -m 512m --memory-reservation=256m nginx

2.2. CPU

2.2. CPU

By default, access to the computing power of the host machine is unlimited. We can set the CPUs limit using the cpus parameter. 

默认情况下,对主机的计算能力的访问是无限的。我们可以使用cpus参数设置CPU的限制。

Let’s constrain our container to use at most two CPUs:

让我们把我们的容器限制在最多使用两个CPU。

$ docker run --cpus=2 nginx

We can also specify the priority of CPU allocation.

我们还可以指定CPU分配的优先级。

The default is 1024, and higher numbers are higher priority:

默认是1024,数字越大优先级越高。

$ docker run --cpus=2 --cpu-shares=2000 nginx

Similar to the memory reservation, CPU shares play the main role when computing power is scarce and needs to be divided between competing processes.

与内存保留类似,当计算能力稀缺并需要在相互竞争的进程之间进行分配时,CPU份额发挥了主要作用。

3. Setting Memory Limit With the docker-compose File

3.用docker-compose文件设置内存限制

We can achieve similar results using docker-compose files. Remember that the format and possibilities will vary between versions of docker-compose.

我们可以使用docker-compose文件实现类似的结果。记住,不同版本的docker-compose的格式和可能性会有所不同。

3.1. Versions 3 and Newer With docker swarm

3.1.使用docker swarm的3和更新版本

Let’s give the Nginx service limit of half of CPU and 512 megabytes of memory, and reservation of a quarter of CPU and 128 megabytes of memory.

让我们给Nginx服务设定一半的CPU和512兆内存的限制,以及四分之一的CPU和128兆内存的保留。

We need to create deploy and then resources segments in our service configuration:

我们需要在服务配置中创建deploy,然后创建resources

services:
  service:
    image: nginx
    deploy:
        resources:
            limits:
              cpus: 0.50
              memory: 512M
            reservations:
              cpus: 0.25
              memory: 128M

To take advantage of the deploy segment in a docker-compose file, we need to use the docker stack command.

为了利用docker-compose文件中的deploy段,我们需要使用docker stack命令。

To deploy a stack to the swarm, we run the deploy command:

为了将堆栈部署到swarm,我们运行deploy命令。

$ docker stack deploy --compose-file docker-compose.yml bael_stack

3.2. Version 2 With docker-compose

3.2.版本2 使用docker-compose

In older versions of docker-compose, we can put resource limits on the same level as the service’s main properties.

在旧版本的docker-compose中,我们可以把资源限制放在与服务的主要属性相同的级别上。

They also have slightly different naming:

它们的命名也略有不同。

service:
  image: nginx
  mem_limit: 512m
  mem_reservation: 128M
  cpus: 0.5
  ports:
    - "80:80"

To create configured containers, we need to run the docker-compose command:

为了创建配置的容器,我们需要运行docker-compose命令。

$ docker-compose up

4. Verifying Resources Usage

4.核实资源使用情况

After we set the limits, we can verify them using the docker stats command:

在我们设置了限制之后,我们可以使用docker stats命令来验证它们。

$ docker stats
CONTAINER ID        NAME                                             CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
8ad2f2c17078        bael_stack_service.1.jz2ks49finy61kiq1r12da73k   0.00%               2.578MiB / 512MiB   0.50%               936B / 0B           0B / 0B             2

5. Conclusion

5.总结

In this article, we explored ways of limiting the docker’s access to the host’s resources.

在这篇文章中,我们探讨了限制docker访问主机资源的方法。

We looked at usage with the docker run and docker-compose commands. Finally, we controlled resource consumption with docker stats.

我们用docker rundocker-compose命令查看了使用情况。最后,我们用docker stats来控制资源消耗。