Graceful shutdown via SIGTERM
Exit code 143 (128 + 15 for SIGTERM) signifies that the container was terminated gracefully. When you run 'docker stop' or 'docker-compose down', Docker first sends a SIGTERM signal to the main process (PID 1) inside the container, giving it a chance to shut down cleanly. If the process exits upon receiving this signal, its exit code will be 143.
- 1Running 'docker stop <container>' command.
- 2Running 'docker-compose down' or 'docker-compose stop'.
- 3The orchestrator (like Kubernetes or Swarm) is stopping the container as part of a deployment or scaling operation.
- 4The container's process correctly handled the SIGTERM signal and exited.
A user manually stops a running Nginx container.
# Start a container in the background docker run -d --name webserver nginx # Stop the container docker stop webserver # Check its exit code docker wait webserver
expected output
143
Fix 1
Implement a Signal Handler
WHEN Your application needs to perform cleanup tasks (e.g., close database connections, save state) before exiting.
# Example in a Node.js application
process.on('SIGTERM', () => {
console.log('SIGTERM signal received. Closing http server.');
server.close(() => {
console.log('Http server closed.');
process.exit(0); // Exit with 0 to indicate success
});
});Why this works
By trapping the SIGTERM signal, the application can execute custom logic to shut down gracefully instead of being abruptly terminated.
Fix 2
Adjust the Stop Grace Period
WHEN Your application needs more (or less) time to shut down than the default 10 seconds.
# Using docker stop with a 30-second grace period
docker stop --time=30 my-container
# In docker-compose.yml
services:
myapp:
image: myapp
stop_grace_period: 30sWhy this works
This overrides the default timeout, giving the application more time to finish its cleanup process before Docker sends a forceful SIGKILL.
✕ Treat Exit Code 143 as an error.
It is the expected and desired outcome for a graceful shutdown initiated by Docker. Alerting on this code will lead to false positives.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev