Docker Container Terminated by SIGTERM (Graceful Shutdown)
Docker exit code 143 indicates that a container received a SIGTERM signal and terminated gracefully. This is typically an expected behavior when Docker or an orchestration system attempts to stop a container, allowing the application inside to perform cleanup before exiting.
Common Causes
- Explicit `docker stop` command issued for the container.
- Container orchestration platforms (e.g., Kubernetes, Docker Swarm) stopping a pod or service.
- Host machine shutdown or restart, leading to Docker stopping all running containers.
- Application inside the container received a SIGTERM from an external process and exited cleanly.
How to Fix
1 Confirm Expected Behavior
In most cases, exit code 143 is not an error but a confirmation of a successful graceful shutdown. Verify if the container was intentionally stopped or if the application handled the SIGTERM as expected by checking container logs.
$ docker logs <container_id_or_name> 2 Adjust Container Stop Timeout
If your application requires more time to shut down gracefully after receiving SIGTERM, you can increase the default stop timeout for Docker. This gives the application more time to complete its cleanup tasks before Docker sends a SIGKILL (exit code 137).
$ docker stop -t 30 <container_id_or_name>
# For Docker Compose:
# stop_grace_period: 30s 3 Implement SIGTERM Handling in Application
Ensure your application running inside the container is designed to properly catch and handle the SIGTERM signal. This allows it to perform necessary cleanup (e.g., flush buffers, close connections, save state) before exiting, ensuring data integrity and a clean shutdown.
$ # Python example:
import signal
import sys
import time
def graceful_shutdown(signum, frame):
print(f'Received signal {signum}, initiating graceful shutdown...')
# Perform cleanup tasks here
time.sleep(5) # Simulate cleanup work
sys.exit(0)
signal.signal(signal.SIGTERM, graceful_shutdown)
print('Application started. Waiting for signals...')
while True:
time.sleep(1)
# Other languages (Node.js, Java, Go) have similar signal handling mechanisms.