Containers Required
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.
Example
# Bad: a shell becomes process 1 and ignores SIGTERM
CMD node server.js
# Good: the app gets the signal
CMD ["node", "server.js"]
services:
web:
init: true
How to check
docker stop on a running container finishes in under 2 seconds, and the app logs its shutdown message.
For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers operations playbook: https://agentwranglers.se/playbook/operations/containers/container-signals.