运行 并且停止 Alpine Docker 容器所花的时间大约是 CentOS、Debian 或 Ubuntu 的 10 倍

Running and stopping an Alpine Docker container takes about 10x as long as CentOS, Debian or Ubuntu

我在 运行 代码中使用不同的 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 存在问题,而其他图像不存在。

如果这是将 alpine linux 图像与 Python 应用程序一起使用,则已知需要更长的时间来构建,尤其是对于更大的图像。

一个建议是剥离 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:7 这样的旧 CentOS 映像也会产生超时。

最后,还不清楚为什么 bash 目前荣誉 SIGTERM(参见 Why does SIGTERM work to terminate bash but not ash or dash?) or why SIGHUP works for busybox sh on the native system but not with docker kill (see )。