I’m using different Docker images to run code and noticed the following:
$ 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
How can this difference be explained?
Update: The time difference is solely produced by docker stop
. If I extend the timeout with docker stop -t 30
for example, the alpine
container takes all of that time and eventually times out, while the behavior of the other containers is naturally not affected by the increased timeout. This seems to have something to do with the propagation of SIGTERM – I can find some previous issues on this, but they are about docker stop
in general and not regarding alpine
explicitly. This doesn’t explain why the issue exists for alpine
and not for the other images.
2
Answers
From the docs for
docker stop
:As opposed to CentOS, Debian and Ubuntu which use
bash
, Alpine usesbusybox sh
which ignoresSIGTERM
and the container is only stopped after the timeout withSIGKILL
.That's different to current versions of
bash
which honor aSIGTERM
and terminate. However, previous versions ofbash
did ignoreSIGTERM
, too: Using an older CentOS image likecentos:7
produces a timeout as well.In the end, it's not clear why
bash
currently honorsSIGTERM
(see Why does SIGTERM work to terminate bash but not ash or dash?) or whySIGHUP
works forbusybox sh
on the native system but not withdocker kill
(see Why does SIGHUP not work on busybox sh in an Alpine Docker container?).If this is using alpine linux image with Python applications, it’s known to take a longer time to build especially with bigger images.
One suggestion is to strip off the alpine base image installation to reduce slow speed times, so remove the originally installed packages or maybe add an option not to cache package downloads using a multi-stage build may work as well.