Sub topic 6
How stop works: SIGTERM, the grace period, and docker kill
This chapter assumes you can start containers with docker run, list them with docker ps, and refer to them by name or ID. It explains what happens inside a container when you run docker stop, why some containers take ten seconds to stop, and when docker kill is the right tool.
What docker stop does
A container is a normal process (or a tree of processes) running on the host. The main process is called PID 1 inside the container. Stopping a container means asking that process to exit.
docker stop works in two steps:
- Docker sends the signal
SIGTERMto PID 1. A signal is a short message the operating system delivers to a process.SIGTERMmeans "please terminate". The process can catch it, save state, close connections, and exit cleanly. - Docker waits for a grace period. The default is 10 seconds. If the process is still running when the period ends, Docker sends
SIGKILL, which the process cannot catch or ignore. The kernel ends it immediately.
Changing the grace period
Use -t (or --time) to change the number of seconds Docker waits.
docker stop -t 30 webThis gives the application 30 seconds to shut down after SIGTERM. Use a longer period for databases or workers that must finish writing or complete a job. A value of 0 skips the wait and sends SIGKILL right away.
Using docker kill
docker kill skips the grace period. By default it sends SIGKILL.
docker kill webThe process gets no chance to clean up. Buffered data may be lost, and lock files or temporary files may be left behind. Use it when a container ignores docker stop, or when you do not care about the container's state.
You can send a different signal with -s. This is useful for applications that react to signals other than SIGTERM.
docker kill -s HUP webMany servers reload their configuration on SIGHUP. The container keeps running, so this does not stop it.
| Command | Default signal | Waits for exit | Cleanup possible |
|---|---|---|---|
| `docker stop` | `SIGTERM`, then `SIGKILL` | Yes, up to the grace period | Yes |
| `docker kill` | `SIGKILL` | No | No |
| `docker kill -s HUP` | `SIGHUP` | No | Depends on the application |
Why a container sometimes takes 10 seconds to stop
If docker stop always takes the full grace period, the main process is probably ignoring SIGTERM. The cause is often how the container starts its process.
The Linux kernel treats PID 1 specially. It does not apply default signal actions to PID 1. A normal program that never installed a SIGTERM handler would die on that signal. As PID 1 in a container, it ignores the signal, so Docker waits for the timeout and then sends SIGKILL.
Two common causes:
- Shell form commands. In a Dockerfile,
CMD npm startruns as/bin/sh -c "npm start". The shell becomes PID 1 and may not forward the signal to your program. Use the exec form,CMD ["npm", "start"], so your program is PID 1. Even then, a wrapper such asnpmmay not forward signals, so runningnode server.jsdirectly is more reliable. - No signal handler. The application must handle
SIGTERMitself. The following Python script shows a handler that makes the container exit at once.
import signal
import sys
import time
def handle_sigterm(signum, frame):
print("Got SIGTERM, shutting down cleanly", flush=True)
sys.exit(0)
signal.signal(signal.SIGTERM, handle_sigterm)
print("Running", flush=True)
while True:
time.sleep(1)Without the signal.signal line, a container running this loop would take the full grace period to stop.
As an alternative, docker run --init starts a small init process as PID 1. It forwards signals to your program and cleans up zombie processes.
Checking the exit code
The exit code tells you how the container ended. Run docker ps -a and read the status, or inspect it directly:
docker inspect --format "{{.State.ExitCode}}" web| Exit code | Meaning |
|---|---|
| `0` | The process exited cleanly, for example after handling `SIGTERM` |
| `143` | Ended by `SIGTERM` (128 + 15) with no handler |
| `137` | Ended by `SIGKILL` (128 + 9), from `docker kill`, an expired grace period, or the out-of-memory killer |
A 137 after docker stop means your application did not exit within the grace period.
Exercise
A container named worker always takes exactly 10 seconds to stop, and docker inspect shows exit code 137. What is happening, and what are two ways to fix it?
Show answer
The main process ignores SIGTERM, so Docker waits out the 10-second grace period and then sends SIGKILL, which gives exit code 137. Fixes: add a SIGTERM handler to the application (and use the exec form of CMD so the application is PID 1), or start the container with docker run --init so an init process forwards signals. Raising -t only helps if the application is actually shutting down slowly.