Docker container 动态修改内存限制

docker动态修改配置用docker update,用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
docker update -h
Flag shorthand -h has been deprecated, please use --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)
--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)
--help Print usage
--kernel-memory string Kernel memory limit
-m, --memory string Memory limit
--memory-reservation string Memory soft limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--restart string Restart policy to apply when a container exits

当前要做的是把一个运行着gitlab 的容器内存限制在2048M以内,尝试用:
docker update -m 2048m gitlab

报错:

1
Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time

发现问题,docker 默认没有启用memory-swap交换内存,直接设置了内存问题会出问题,也就是说宿主 swap 支持使用多少则容器即可使用多少,如果 –memory-swap 设置小于 –memory则设置不生效。

将memory-swap 设置值为 -1,表示容器程序使用内存受限,而 swap 空间使用不受限制。

1
docker update --memory 2048m --memory-swap -1 gitlab

问题解决。

shikanon wechat
欢迎您扫一扫,订阅我滴↑↑↑的微信公众号!