我了解到 docker run -m 256m --memory-swap 256m
将限制一个容器,以便它最多可以使用 256 MB 的内存并且没有交换空间。如果它分配更多,那么容器中的进程(不是“容器”)将被杀死。例如:
$ sudo docker run -it --rm -m 256m --memory-swap 256m \
stress --vm 1 --vm-bytes 2000M --vm-hang 0
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: FAIL: [1] (415) <-- worker 7 got signal 9
stress: WARN: [1] (417) now reaping child worker processes
stress: FAIL: [1] (421) kill error: No such process
stress: FAIL: [1] (451) failed run completed in 1s
显然,其中一名工作人员分配的内存超出了允许范围,并收到了 SIGKILL
。请注意,父进程保持事件状态。
现在,如果 -m
的效果是在进程分配过多内存时调用 OOM killer ,那么指定 -m
时会发生什么 和 --oom-kill-disable
?像上面那样尝试有以下结果:
$ sudo docker run -it --rm -m 256m --memory-swap 256m --oom-kill-disable \
stress --vm 1 --vm-bytes 2000M --vm-hang 0
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
(waits here)
在不同的 shell 中:
$ docker stats
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
f5e4c30d75c9 0.00% 256 MiB / 256 MiB 100.00% 0 B / 508 B 0 B / 0 B 2
$ top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
19391 root 20 0 2055904 262352 340 D 0.0 0.1 0:00.05 stress
我看到 docker stats
显示内存消耗为 256 MB,top
显示 RES
为 256 MB 和 2000 MB 的 VIRT
。但是,这究竟意味着什么?如果容器内的进程尝试使用超过允许的内存,会发生什么情况?在什么意义上它受 -m
约束?
最佳答案
据我了解the docs --oom-kill-disable
不受 -m
的约束,但实际上需要它:
By default, kernel kills processes in a container if an out-of-memory (OOM) error occurs. To change this behaviour, use the --oom-kill-disable option. Only disable the OOM killer on containers where you have also set the -m/--memory option. If the -m flag is not set, this can result in the host running out of memory and require killing the host’s system processes to free memory.
一位开发者说 back in 2015那个
The host can run out of memory with or without the -m flag set. But it's also irrelevant as --oom-kill-disable does nothing unless -m is passed.
关于您的更新,当 OOM-killer
被禁用但内存限制已达到(intresting OOM article)时会发生什么,id 表示对 malloc
这样就会像描述的那样失败 here但它也取决于交换配置和主机可用内存。如果您的 -m
限制高于实际可用内存,主机将开始终止进程,其中之一可能是 docker 守护进程(他们试图通过 changing its OOM priority 避免)。
kernel docs (cgroup/memory.txt
) 说
If OOM-killer is disabled, tasks under cgroup will hang/sleep in memory cgroup's OOM-waitqueue when they request accountable memory
对于 cgroups 的实际实现(docker 也使用),您必须 check the sourcecode .
关于linux - `--oom-kill-disable` 对 Docker 容器有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48618431/