Let's cut the fluff. Here is exactly what you need to know about this concept to survive in a real production environment.
1The Violent Kill
Look, if you've ever dealt with this in production, you know exactly what the problem is. When you update your application, Docker must stop the old container and start the new one. If Docker simply unplugs the power cord (a SIGKILL), the results are disastrous. Any user currently downloading a file gets abruptly disconnected. Any database transaction halfway through writing data is corrupted. To safely update infrastructure, you must gracefully shut down your containers without interrupting active users. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior engineers. When you deploy to a cluster, this is the mechanic that prevents catastrophic failure.
# User is downloading a 1GB file...
# Docker issues SIGKILL (Kill immediately)
# Connection Severed!
# Download fails, Database corrupted.
Status: OK
Success: Operation completed.
2SIGTERM: The Polite Warning
Look, if you've ever dealt with this in production, you know exactly what the problem is. Docker was designed to be polite. When you run docker stop, Docker does NOT kill the container instantly. It sends a SIGTERM (Signal Terminate) to PID 1. This is a polite request: 'Please shut down soon.' Your application must listen for this signal. When it hears the SIGTERM, it should immediately stop accepting NEW traffic, but allow EXISTING users to finish their downloads and database transactions. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior engineers. When you deploy to a cluster, this is the mechanic that prevents catastrophic failure.
> docker stop my-api
# 1. Docker sends SIGTERM
# 2. App stops taking NEW requests
# 3. App finishes CURRENT requests
# 4. App gracefully closes DB connections
# 5. App exits cleanly (Exit Code 0)
Status: OK
Success: Operation completed.
3Trapping the Signal in Code
Look, if you've ever dealt with this in production, you know exactly what the problem is. Docker sends the signal, but your code must actually catch it. In Node.js, you use process.on('SIGTERM'). Inside this function, you tell your HTTP server to server.close(). This brilliant command stops accepting new network connections but keeps the server alive until all current requests are completely finished. Once finished, you disconnect the database and call process.exit(0). This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior engineers. When you deploy to a cluster, this is the mechanic that prevents catastrophic failure.
process.on('SIGTERM', () => {
console.log('SIGTERM received. Shutting down gracefully...');
// Stop new connections, finish existing ones
server.close(() => {
console.log('All active requests finished.');
db.disconnect();
process.exit(0); // Safely power off
});
});
The server returned a 200 OK HTTP response.
