docker之动态扩容

背景

在用docker进行项目编译的时候,出现性能不足的情况,

1
2
3
4
5
6
7
8
9
10
11
12
13
zhengkaideMacBook-Pro:blog zhengk$ docker stats b9e5ebd23a78
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b9e5ebd23a78 wizardly_meninsky 2.65% 1.752GiB / 1.952GiB 89.74% 965MB / 23.1MB 282GB / 7.76GB 85
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b9e5ebd23a78 wizardly_meninsky 2.65% 1.752GiB / 1.952GiB 89.74% 965MB / 23.1MB 282GB / 7.76GB 85
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b9e5ebd23a78 wizardly_meninsky -- -- / -- -- -- -- --
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b9e5ebd23a78 wizardly_meninsky -- -- / -- -- -- -- --
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b9e5ebd23a78 wizardly_meninsky -- -- / -- -- -- -- --
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b9e5ebd23a78 wizardly_meninsky -- -- / -- -- -- -- --

通过命令都无法显示性能情况。

不过可见,最大内存限制在1.952GiB。

动态扩容

  • 先找到docker容器的id

    1
    2
    3
    zhengkaideMacBook-Pro:blog zhengk$ docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    b9e5ebd23a78 hub.baidubce.com/paddlepaddle/paddle:latest-dev "/bin/bash" 4 days ago Up 2 days 22/tcp wizardly_meninsky
  • 进行扩容

    1
    2
    zhengkaideMacBook-Pro:blog zhengk$ docker update -m 3072m b9e5ebd23a78
    Error response from daemon: Cannot update container b9e5ebd23a78843361c3f92dbdb7b07dcf01a85087c4c7b14670dc5cc440433c: Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time

    注:这里有一个错误提示,说要同步扩大memoryswap。

    命令修改如下:

    1
    2
    zhengkaideMacBook-Pro:blog zhengk$ docker update -m 3072m --memory-swap -1 b9e5ebd23a78
    b9e5ebd23a78
  • 再次查看

    1
    2
    3
    zhengkaideMacBook-Pro:blog zhengk$ docker stats b9e5ebd23a78
    CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
    b9e5ebd23a78 wizardly_meninsky 2.65% 1.752GiB / 1.952GiB 89.74% 965MB / 23.1MB 282GB / 7.76GB 85

    发现没有什么变化,限制还是1.952GiB。

    这是由于docker在MacDesktop环境下默认限制了上限为2G。

    Advanced settings are:

    CPUs: By default, Docker Desktop for Mac is set to use half the number of processors available on the host machine. To increase processing power, set this to a higher number; to decrease, lower the number.

    Memory: By default, Docker Desktop for Mac is set to use 2 GB runtime memory, allocated from the total available memory on your Mac. To increase RAM, set this to a higher number; to decrease it, lower the number.

    Swap: Configure swap file size as needed. The default is 1 GB.

  • 修改Docker Desktop For Mac

    image-20190819234844808

  • 重启容器并查看

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    zhengkaideMacBook-Pro:blog zhengk$ docker stop b9e5ebd23a78
    b9e5ebd23a78
    zhengkaideMacBook-Pro:blog zhengk$ docker start b9e5ebd23a78
    b9e5ebd23a78
    zhengkaideMacBook-Pro:blog zhengk$ docker stats b9e5ebd23a78
    CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
    b9e5ebd23a78 wizardly_meninsky 0.00% 3.102MiB / 3GiB 0.10% 1.16kB / 0B 11.5MB / 0B 1
    CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
    b9e5ebd23a78 wizardly_meninsky 0.00% 3.055MiB / 3GiB 0.10% 1.16kB / 0B 11.5MB / 0B 1
    CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
    b9e5ebd23a78 wizardly_meninsky 0.00% 3.055MiB / 3GiB 0.10% 1.16kB / 0B 11.5MB / 0B 1
    CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
    b9e5ebd23a78 wizardly_meninsky 0.00% 3.055MiB / 3GiB 0.10% 1.16kB / 0B 11.5MB / 0B 1

扩展

对于桌面版的docker,还是要关注一下系统的配置,开始找了好久死活都没办法扩容。

对于常规的扩容,可参考帮助:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
zhengkaideMacBook-Pro:blog zhengk$ docker update --help

Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]

Update configuration of one or more containers

Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--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 the CPU real-time period in microseconds
--cpu-rt-runtime int Limit the 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 (0-3, 0,1)
--kernel-memory bytes Kernel memory limit
-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
--pids-limit int Tune container pids limit (set -1 for unlimited)
--restart string Restart policy to apply when a container exits
zhengkaideMacBook-Pro:blog zhengk$

使用命令动态修改之后,如果没重启的话

1
2
3
4
5
6
7
zhengkaideMacBook-Pro:blog zhengk$ docker stats b9e5ebd23a78
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b9e5ebd23a78 wizardly_meninsky 0.00% 0B / 0B 0.00% 0B / 0B 0B / 0B 0
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b9e5ebd23a78 wizardly_meninsky 0.00% 0B / 0B 0.00% 0B / 0B 0B / 0B 0
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b9e5ebd23a78 wizardly_meninsky 0.00% 0B / 0B 0.00% 0B / 0B 0B / 0B 0

无法直观的看到有没有生效,所以建议还是重启一次。