Solution:
Root Cause: Forced SIGKILL Termination Following Shutdown Timeout
When Docker, Docker Swarm, or Kubernetes attempts to stop a container, it sends
SIGTERM and waits for a graceful shutdown period (default 10 seconds). If the application fails to handle
SIGTERM or takes too long to clean up resources, the daemon sends
SIGKILL (signal 9), resulting in exit code 137.
Diagnostic Verification:
1Inspect events log to determine if exit 137 was preceded by a stop command:
bash
docker events --until 0m --filter event=die
2Review application logs to check if SIGTERM was received but ignored.
Step-by-Step Fix:
1Increase Stop Timeout in Docker Run / Compose:
Allow more time for graceful shutdown: bash
docker run --stop-timeout 30 my-app:latest
In docker-compose.yml: yaml
services:
app:
image: my-app:latest
stop_grace_period: 30s
2Handle SIGTERM inside Application Code:
Ensure main process (PID 1) catches SIGTERM signals and initiates graceful shutdown of active connections.3Use Tini or Dumb-Init Init System:
If app runs as PID 1 without signal handlers, add an init wrapper: dockerfile
RUN apk add --no-cache tini
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "server.js"]
Prevention & Long-Term Monitoring:
Always test process signal handling during integration testing.