An idle container consistently takes about ten seconds to stop. What is Docker waiting for, and how can I find out what is causing the delay?
Why does stopping an idle Docker container take ten seconds?
Answers
Docker normally sends the container's stop signal, waits for the grace period, then sends SIGKILL if the container is still running. Ten seconds is the usual default on Linux, so a very consistent delay around that mark is a good clue.
On Linux, PID 1 inside a PID namespace has special signal behavior. If it hasn't installed a handler for the stop signal, the kernel won't deliver that signal to it from an ancestor namespace. A plain sleep process running as PID 1 can therefore sit there until Docker sends SIGKILL, even though the same process would normally terminate on SIGTERM outside that role.
Reply to this answer
Check what actually runs as PID 1. If an entrypoint script starts the application as a child and leaves the shell in charge, the application may not receive the stop signal.
For a script that starts one long-running application, use exec for that final command. That replaces the shell with the application, so the application becomes PID 1 and can handle the signal itself. It still needs to handle the configured stop signal properly.
_y39+drainwicket.8:
Don't assume a shell command always leaves the shell as PID 1. Some shells replace themselves with the last command in certain cases, while others don't. Check the actual process tree.
Reply to this answer
For a more direct check on a Linux host, get the container's host PID with docker inspect and look at SigCgt in that process's /proc/PID/status file. It's a hexadecimal mask of signals with installed handlers. For SIGTERM, signal 15, the mask to check is 0x4000.
First check the image's configured stop signal though. It isn't always SIGTERM. An image can set STOPSIGNAL to something else, so checking the SIGTERM bit alone can send you down the wrong path. Also, a handler being present doesn't prove the application will shut down promptly.
Reply to this answer
This can show up during container recreation too, not just when you run stop by hand. If the old container waits out its grace period every time, that delay can add up during redeploys. Fix the signal handling rather than assuming the deployment tool is slow.
Reply to this answer
Add your answer
Answers are reviewed before publication.
8h!datacog_517:
You can shorten the wait with the stop timeout, but that just makes the forced kill happen sooner. It doesn't give the application a graceful shutdown.