我正在使用不同的 Docker 镜像来运行代码并注意到以下内容:
$ time sudo docker stop $(sudo docker run -dit --rm centos:latest)
509285200bcf4ea8389219319b6c4af6554abf9f1c9d9ffb152cd109b6a21453
real 0m1,374s
user 0m0,087s
sys 0m0,027s
$ time sudo docker stop $(sudo docker run -dit --rm debian:latest)
221221b4b9238de633ca68d9539b024c06d015ea503a8b7fd5b827dc903193b8
real 0m1,345s
user 0m0,086s
sys 0m0,034s
$ time sudo docker stop $(sudo docker run -dit --rm ubuntu:latest)
5512fb2505f28f7d2a52788a2ed9775c78ae40805da44e51bb3f22073133f19c
real 0m1,341s
user 0m0,075s
sys 0m0,048s
$ time sudo docker stop $(sudo docker run -dit --rm alpine:latest)
dc1775fa2734c753a5ba6f3fc0b14bf356376d8c701fcf6efa637f1795f41b4a
real 0m11,439s
user 0m0,089s
sys 0m0,032s
如何解释这种差异?
更新:时差完全由
docker stop
产生。 .如果我用 docker stop -t 30
延长超时例如,alpine
容器占用了所有这些时间并最终超时,而其他容器的行为自然不受超时增加的影响。这似乎与 SIGTERM 的传播有关 - 我可以找到一些以前的问题,但它们大约是 docker stop
一般而言,与 alpine
无关明确地。这不能解释为什么 alpine
存在问题。而不是其他图像。
最佳答案
来自 docker stop
的文档:
The main process inside the container will receive
SIGTERM
, and after a grace period,SIGKILL
.
与使用
bash
的 CentOS、Debian 和 Ubuntu 不同, Alpine 使用 busybox sh
忽略 SIGTERM
并且容器仅在超时后停止 SIGKILL
.这与
bash
的当前版本不同。荣誉SIGTERM
并终止。但是,bash
的早期版本确实忽略了SIGTERM
,同样:使用较旧的 CentOS 镜像,如 centos:7
也会产生超时。最后,不清楚为什么
bash
目前荣誉SIGTERM
(见 Why does SIGTERM work to terminate bash but not ash or dash? )或为什么 SIGHUP
为 busybox sh
工作在 native 系统上,但不使用 docker kill
(见 Why does SIGHUP not work on busybox sh in an Alpine Docker container?)。
关于docker - 运行和停止 Alpine Docker 容器的时间大约是 CentOS、Debian 或 Ubuntu 的 10 倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60493765/