Docker 容器资源(内存、CPU)限制

目标

容器运行过程中可以内存不足限制Docker容器内存,使得Docker容器中的应用运行更安全。

命令

Docker 提供了限制内存的命令

  1. 启动时资源限制
    可以通过 docker run --help 命令查看

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping
                                       (host:ip)
    # CPU相关可选参数
    -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight),
                                       between 10 and 1000, or 0 to
                                       disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device
                                       weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the
                                       container
      --cgroupns string                Cgroup namespace to use
                                       (host|private)
                                       'host':    Run the container in
                                       the Docker host's cgroup namespace
                                       'private': Run the container in
                                       its own private cgroup namespace
                                       '':        Use the cgroup
                                       namespace as configured by the
                                                  default-cgroupns-mode
                                       option on the daemon (default)
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair
                                       Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair
                                       Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in
                                       microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in
                                       microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution
                                       (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution
  …… ……
  # 内存限制命令参数,使用格式 --key=value 如:--memory=10g --memory-swap=16G 
  -m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus
                                       swap: '-1' to enable unlimited swap
      --memory-swappiness int          Tune container memory swappiness
                                       (0 to 100) (default -1)
      --mount mount                    Attach a filesystem mount to the
                                       container
      --name string                    Assign a name to the container
      --network network                Connect a container to a network
      --network-alias list             Add network-scoped alias for the
                                       container
      --no-healthcheck                 Disable any container-specified
                                       HEALTHCHECK
      --oom-kill-disable               Disable OOM Killer
      --oom-score-adj int              Tune host's OOM preferences (-1000
                                       to 1000)
      --pid string                     PID namespace to use
      --pids-limit int                 Tune container pids limit (set -1
                                       for unlimited)
      --platform string                Set platform if server is
                                       multi-platform capable
      --privileged                     Give extended privileges to this
 …… ……

示例:

# 限制内存命令
docker run -it --name alpine-test -m=4g alpine
# 或
docker run -it --memory=4G --swap-memory=4G --name alpine-test alpine
  1. 运行中容器可以使用 docker update 命令
    同样可以使用 docker update --help 查看到资源限制可选参数。其含义,表达格式与 docker run 命令类似。

  2. 通过创建sawp文件扩展Docker容器中内存
    在尝试源码编译安装MongoDB 时遇到内存不足的问题,可以在用这种方法解决。
    增大交换区大小,如下:

#count的大小就是增加的swap空间的大小,64M是块大小,所以空间大小是bs*count=2048MB
dd if=/dev/zero of=/swapfile bs=64M count=32

#把刚才空间格式化成swap格式
mkswap /swapfile

chmod 0600 /swapfile  
# 使用刚才创建的swap空间
swapon /swapfile

如果是临时扩展交换空间,用完后应及时归还,归还命令swaoff /swapfile。这种方式在Win端不可以行,实践时执行到swapon /swapfileswapoff /swapfile 时都会报 错

  • swapon failed: Invalid argument 这种场景请使用Win下修改配置文件的方式。
  • 没有权限permission denied, 这种应在run时添加参数 --privileged=true

现状

现存的Win、Mac、Linux三种流行的操作系统,其Docker容器内存限制方式都有差异。

Win

Win 下一般通过安装Docker Desktop 安装Docker环境。可以在Docker Desktop中直接配置,配置文件有两种,分别是.wslconfigwsl.conf.wslconfig是全局配置文件,配置后所有容器生效;wsl.conf是局部配置文件,对应于单一容器配置。其中.wslconfig配置文件可以如下查找:
在这里插入图片描述
以上方式打开的文档是英文的,中文文档可以访问https://docs.microsoft.com/zh-cn/windows/wsl/wsl-config
配置文件如果不存在,可以手动创建。
这种方式配置完成后应重启Docker Desktop 或重启电脑。

Mac

Mac 请参考文档: https://docs.docker.com/docker-for-mac/#memory

Linux

采用官方提供的命令解决,即前文命令章节提到的三种方式可按需尝试。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值