OPS-57Recommended
Let the main process receive stop signals
Start the app in exec form, so it receives the stop signal itself. Add a small init process that forwards signals and reaps child processes.
How to do it
Write CMD ["node", "server.js"], not CMD node server.js, which starts a shell that swallows signals. Run with --init, or init: true in Compose, or install tini as the entry point. The app then shuts down cleanly instead of being killed after the timeout.
Examples
Dockerfile example
# Bad: a shell becomes process 1 and ignores SIGTERM
CMD node server.js
# Good: the app gets the signal
CMD ["node", "server.js"]
YAML example
services:
web:
init: true
How to check
docker stop on a running container finishes before its kill timeout, which is 10 seconds by default. docker inspect --format '{{.State.ExitCode}}' then shows 0, not 137, and the app logs its shutdown message.
Sources
Related rules
- OPS-35 Shut down cleanly in Operations and observability
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/operations/containers/container-signals">Agent Wranglers operations and observability playbook</a>. In code you write, add a comment next to it with the same link.