Docker中设置Memory和CPU限制

Setting Memory And CPU Limits In Docker

Last modified: December 9, 2020

by Michał Dąbrowski

1. Overview

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

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

2. Setting Resources Limit With 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.

2.1. Memory

For instance, let's limit the memory that the container can use to 512 megabytes. To constrain memory, we need to use the m parameter:

$ 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 run -m 512m --memory-reservation=256m nginx

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. For example, let's constrain our container to use at most two CPUs:

$ docker run --cpus=2 nginx

We can also specify the priority of CPU allocation. The default is 1024, higher numbers are higher priority:

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

3. Setting Memory Limit With the docker-compose File

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

3.1. Versions 3 and Newer With docker swarm

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. We need to create “deploy” and then “resources” segments in our service configuration:

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. To deploy a stack to the swarm, we run the deploy command:

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

3.2. Version 2 With docker-compose

In older versions of docker-compose, we can put resource limits on the same level as the service's main properties. 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 up

4. Verifying Resources Usage

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

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

In this tutorial, we explored ways of limiting the docker's access to the host's resources. We looked at usage with the docker run and docker-compose commands. Finally, we controlled resource consumption with docker stats.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值