Instead of typing “sudo reboot” I reboot my machine with a script where I call “docker stop” for each container first with a long timeout, so all my containers have enough time to gracefully shutdown, before finally executing “sudo reboot”.
The problem is that after reboot the containers with the “unless stopped” policy are not started. This is because Docker thinks I manually stopped them instead of rebooting.
So is there a way to add a flag to “docker stop” so that it knows to count it as an automatic stop instead of a manual one?
Containers should gracefully stop unless there is timeout on the system. The timeout is not specified in in the systemd service:
Since I don’t know how Docker knows if the container was stopped interactively or just finished its job, I am not sure it would work, but Ii there is an other timeout and you still need to manually stop containers, you can use your script to inspect the containers and get the stop signal, the process id and the stop timeout, then send the stop signal from your script directly to the process without using docker stop and handle the stop timeout yourself.
Thanks, but I have no problem with stopping the containers?
My problem is that I have to manually start them after reboot when they have the “unless-stopped” policy.
That is because docker thinks I manually stopped them, so I need some way to tell docker that the stop was a “automatic” one.
Im the lats 8 years I never stopped containers explicity before stopping or restarting the host…
Docker works as is supposed to work. Just because you differentiate between a container stopped within your script or from the cli, doesn’t mean both are handled diffenently.
Is there a specific reason that you need to stop the containers within your script (which I would also call manually, even though it is in a script)?
Some of the containers take a long time to stop gracefully, like Bitcoin for example.
If I reboot docker will kill it after a few seconds, which can lead to a corrupted database or files. That’s why I use the script to give them more time to shutdown normally without being killed.
I assume you have seen the discussion in this GH issue: dockerd shutdown-timeout option does not work as expected · Issue #32357 · moby/moby · GitHub regarding the
shutdown-timeout parameter of the docker engine?
Yes, you have. Quote
docker stop does not have such a flag, I recommended not using
docker stop so if Docker stores how you stopped a container, it cannot store when you send signals directly to the processes inside a container.
When I set stop timeout to 40 seconds in a container, stopping the docker daemon took 40 seconds. So actually if something kills Docker before stopping each container when you shut down the machine, I would rather stop the deamon before shutting down instead of individually stopping each container.
I found it too, because I wanted to test it before I recommend that. I stopped the docker deamon but
shutdown-timeout did not affect speed of stopping the daemon. I didn’t try to actually shut down my computer since it was a Digital ocean droplet.
Do you really have increased stop timeout for those containers? It would probably not matter if you manually run
docker stop or docker daemon stops those containers, except if Docker stops all containers at the same time and it somehow makes the containers slower.