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:
1.Inspect events log to determine if exit 137 was preceded by a stop command:
bash
docker events --until 0m --filter event=die
2.Review application logs to check if SIGTERM was received but ignored.
Step-by-Step Fix:
1.Increase 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
2.Handle SIGTERM inside Application Code:
Ensure main process (PID 1) catches SIGTERM signals and initiates graceful shutdown of active connections.3.Use 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.