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 run docker stop and docker start by name. It explains what happens between you pressing Enter on docker stop and the container reaching the exited state. You will learn which signals are sent, how long Docker waits, why some containers ignore docker stop, and when docker kill is the right tool.
Signals in a container
A signal is a small message the operating system delivers to a process. A process can handle most signals with its own code, for example by saving data and exiting. Two signals cannot be handled: SIGKILL and SIGSTOP. The kernel enforces them.
Docker uses signals to end containers. A container is a group of isolated processes, and the first process started inside it is called the main process. Docker records it as PID 1 (process ID 1) inside the container. When the main process exits, the container stops. Docker sends its stop signals to that process.
| Signal | Number | Can the process handle it? | Typical meaning |
|---|---|---|---|
| `SIGTERM` | 15 | Yes | Please shut down cleanly |
| `SIGINT` | 2 | Yes | Interrupt, as with Ctrl+C |
| `SIGKILL` | 9 | No | End immediately |
| `SIGHUP` | 1 | Yes | Hang up, often "reload configuration" |
What docker stop does
docker stop performs a two-step shutdown:
- It sends
SIGTERMto the main process of the container. - It waits for the grace period. The default is 10 seconds.
- If the process is still running when the period ends, it sends
SIGKILL.
If the process exits during the grace period, Docker does not send SIGKILL. A well-behaved server catches SIGTERM, finishes in-flight requests, closes connections, flushes buffers, and exits. That is why docker stop is the normal way to end a container.
The command prints the container name or ID when it finishes. The exit code of the container tells you which path was taken. You can read it with docker inspect.
Setting the grace period
Use -t (or --time) to change the wait, in seconds.
docker run -d --name web nginx:alpine
docker stop -t 30 webA database that needs time to flush to disk may need 30 seconds or more. A stateless worker may be fine with 2. A value of 0 sends SIGKILL right after SIGTERM with no wait. A value of -1 waits indefinitely, which is useful only when you are sure the process will exit.
You can also set a default per container when you create it:
docker run -d --name db --stop-timeout 60 postgres:16If you do not pass -t to docker stop, Docker uses the container's own stop timeout. In a Dockerfile, the STOPSIGNAL instruction sets the signal, and the --stop-timeout option on docker run sets the timeout.
Choosing a different stop signal
Some programs treat SIGTERM as something other than "shut down". Nginx, for example, handles SIGQUIT as a graceful shutdown and SIGTERM as a fast shutdown. The official nginx image sets STOPSIGNAL SIGQUIT for this reason. You can set it yourself in a Dockerfile:
FROM alpine:3.20
STOPSIGNAL SIGINT
CMD ["sleep", "infinity"]You can also override it at run time:
docker run -d --name worker --stop-signal SIGINT alpine sleep infinitydocker stop then sends SIGINT instead of SIGTERM. The grace period and the final SIGKILL work the same way.
docker kill
docker kill skips the grace period. By default it sends SIGKILL at once:
docker kill webThe process gets no chance to run cleanup code. Open files may be left half-written, and network clients see a dropped connection. Use it when a container is stuck, or when you do not care about its state.
docker kill can send other signals with -s. This makes it a way to talk to a running process, not only to end it:
docker kill -s HUP web
docker kill -s USR1 webSending HUP to nginx asks it to reload its configuration without stopping the container. Whether a signal does anything depends on the program. If the process has no handler for it, the default action applies, which is often to terminate.
| Command | Signal sent | Waits? | Cleanup runs? |
|---|---|---|---|
| `docker stop web` | `SIGTERM`, then `SIGKILL` if needed | Up to 10 s by default | Yes, if the program handles `SIGTERM` |
| `docker stop -t 0 web` | `SIGTERM`, then `SIGKILL` at once | No | Practically no |
| `docker kill web` | `SIGKILL` | No | No |
| `docker kill -s HUP web` | `SIGHUP` | No | Not a stop; the container keeps running unless the program exits |
Reading the exit code
When a process ends from a signal, its exit code is 128 plus the signal number. That gives you a way to see how a container ended.
docker run -d --name web nginx:alpine
docker stop web
docker inspect web --format '{{.State.ExitCode}}'| Exit code | Meaning |
|---|---|
| `0` | The process exited normally, usually after handling `SIGTERM` or `SIGQUIT` |
| `143` | Terminated by `SIGTERM` (128 + 15), with no handler |
| `137` | Terminated by `SIGKILL` (128 + 9) |
| `130` | Terminated by `SIGINT` (128 + 2) |
An exit code of 137 after docker stop is a warning sign. It means the process did not exit within the grace period, and Docker had to kill it. The same code can also appear when the kernel kills a container for running out of memory. To tell the two apart, check docker inspect web --format '{{.State.OOMKilled}}'. It prints true for an out-of-memory kill.
Why a container ignores SIGTERM
The most common failure is a container that always takes the full 10 seconds to stop. You will see a pause, and then exit code 137. This happens for two main reasons.
PID 1 has no default signal handlers
On Linux, the kernel treats PID 1 in a PID namespace specially. A signal that has no handler installed is ignored for PID 1, instead of falling back to the default action of terminating the process. A normal program does not notice this, because shells and many tools install handlers. A small program that never registers a SIGTERM handler will not exit when it receives one if it runs as PID 1.
Here is a Python script that shows the problem and the fix:
import signal
import sys
import time
def shutdown(signum, frame):
print("received signal", signum, "cleaning up", flush=True)
sys.exit(0)
signal.signal(signal.SIGTERM, shutdown)
print("running", flush=True)
while True:
time.sleep(1)Save it as app.py and build an image:
FROM python:3.12-alpine
COPY app.py /app.py
CMD ["python", "/app.py"]docker build -t signal-demo .
docker run -d --name demo signal-demo
time docker stop demo
docker logs demo
docker inspect demo --format '{{.State.ExitCode}}'The stop returns in about a second or less. The log shows received signal 15 cleaning up, and the exit code is 0. If you remove the line with signal.signal, the stop takes the full 10 seconds and the exit code is 137.
The shell form of CMD hides your program
A Dockerfile CMD or ENTRYPOINT can be written in two forms.
- Exec form:
CMD ["python", "/app.py"]. Docker runs your program directly, and it becomes PID 1. - Shell form:
CMD python /app.py. Docker runs/bin/sh -c "python /app.py". The shell becomes PID 1 and your program is its child.
Many shells do not pass SIGTERM on to their children. The shell receives the signal and your program never sees it. After the grace period, Docker sends SIGKILL, which ends the shell and, with it, the container.
The fix is to use exec form. If you need a shell script as the entry point, end it with exec, so the shell replaces itself with your program:
#!/bin/sh
echo "starting"
exec python /app.pyUsing an init process
Another fix is to run a tiny init process as PID 1. It forwards signals to your program and reaps zombie child processes. Docker includes one, based on tini, which you enable with --init:
docker run -d --init --name demo2 signal-demoThis is useful when you cannot change the program or when it spawns many child processes.
Related commands
Several other commands affect shutdown, and it helps to know how they relate to the signals above.
docker restartstops the container with the sameSIGTERM, grace period, andSIGKILLsequence, then starts it. It accepts-ttoo.docker stopaccepts several containers:docker stop web db cache. Docker stops them one after another, not at the same moment, so a long grace period on each adds up.docker rm -fsendsSIGKILLto a running container before removing it. It does not allow a graceful shutdown.- Stopping the Docker daemon, or restarting the host, triggers the same stop sequence on running containers, unless live restore is enabled.
Common mistakes
- Using
docker killas a routine way to stop containers. Data in databases and message queues can be left in an inconsistent state. - Setting
-t 0to make tests faster, and then losing log output or in-progress writes. - Writing shell form
CMDand wondering why each stop takes 10 seconds. - Raising the timeout to hide a program that never handles
SIGTERM. The better fix is to handle the signal. - Reading exit code 137 as always meaning "out of memory". Check
OOMKilledbefore drawing conclusions.
Summary of the model
docker stopsends the stop signal (SIGTERMby default), waits for the grace period (10 seconds by default), and then sendsSIGKILL.docker killsendsSIGKILLright away, or any signal you choose with-s.- Docker delivers signals only to PID 1 of the container. Exec form
CMD, anexecin your entry script, or--initmake sure your program receives them. - Exit codes 143 and 137 tell you how the process ended, and
OOMKilledseparates a timeout kill from a memory kill.
Exercise
You run docker stop api and the command takes about 10 seconds. Afterward, docker inspect api --format '{{.State.ExitCode}}' prints 137, and OOMKilled is false. The Dockerfile ends with CMD node server.js. What is the most likely cause, and what are two ways to fix it?
Show answer
The shell form of CMD runs /bin/sh -c "node server.js", so the shell is PID 1 and does not forward SIGTERM to Node. After the 10-second grace period Docker sends SIGKILL, which gives exit code 137. Fix it by changing to exec form, CMD ["node", "server.js"], so Node is PID 1 (and add a SIGTERM handler in the program if it does not have one). Alternatively, run the container with --init so an init process forwards the signal.
Exercise
A container runs a database that needs up to 45 seconds to flush data on shutdown. Write the command to create it so that every docker stop allows that time, and then write the command to send it a SIGHUP without stopping it.
Show answer
Create it with docker run -d --name db --stop-timeout 60 postgres:16 (any value above 45 works). After that, docker stop db waits up to 60 seconds before SIGKILL. Send the signal with docker kill -s HUP db. The container keeps running unless the program chooses to exit on SIGHUP.