Sub topic 14
Signals and PID 1 in containers
This chapter assumes you know how shell form and exec form differ, and how ENTRYPOINT and CMD set a container's startup command. It explains why that choice decides whether docker stop shuts your program down cleanly or kills it after a delay.
What PID 1 means
Every process in a Linux container namespace has a process ID. The first process the container starts gets PID 1. For your image, that is whatever ENTRYPOINT and CMD resolve to.
PID 1 is special in two ways:
- The kernel does not apply default signal actions to it. For most processes, SIGTERM terminates them even if the program never handles it. PID 1 ignores a signal unless the program installed its own handler for it.
- It is responsible for reaping orphaned child processes. If it never collects them, they remain as zombie entries.
What docker stop does
docker stop sends SIGTERM to PID 1 and waits 10 seconds by default. If the container is still running, Docker sends SIGKILL, which cannot be caught. A program that ignores SIGTERM therefore always takes the full 10 seconds to stop, and it gets no chance to flush data or close connections.
Why shell form breaks signals
Shell form wraps your command in /bin/sh -c. The shell becomes PID 1, and your program is its child. The shell does not forward SIGTERM to the child, so the program never sees the signal.
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
# Shell form: /bin/sh is PID 1, python is its child
CMD python app.pyExec form starts the program directly, so it is PID 1 and receives signals from Docker.
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
# Exec form: python is PID 1
CMD ["python", "app.py"]Handling SIGTERM in your program
Being PID 1 also means the program needs its own handler. This script exits cleanly on SIGTERM:
import signal
import sys
import time
def handle_sigterm(signum, frame):
print("received SIGTERM, shutting down", flush=True)
sys.exit(0)
signal.signal(signal.SIGTERM, handle_sigterm)
print("running", flush=True)
while True:
time.sleep(1)Save it as app.py and build the exec-form image above. Run it, then stop it from another terminal and time the stop.
docker build -t sigdemo .
docker run -d --name sig sigdemo
time docker stop sig
docker logs sig
docker rm sigThe stop returns almost at once and the log shows the shutdown message. With the shell-form version, docker stop takes about 10 seconds and the message never appears.
Shell scripts as entrypoints
A common pattern is an ENTRYPOINT script that prepares configuration and then starts the real program. End such a script with exec, which replaces the shell with the program instead of starting a child.
#!/bin/sh
set -e
echo "preparing configuration"
exec "$@"Without exec, the shell stays as PID 1 and the final program does not get signals.
Using an init process
If your program starts child processes, or cannot handle signals, run a small init program as PID 1. It forwards signals and reaps zombies. Docker includes one, enabled with --init:
docker run --init --rm sigdemoThe --init flag runs a small init program (tini) as PID 1, with your command as its child.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `docker stop` always takes 10 seconds | Shell form, or no SIGTERM handler | Use exec form and add a handler |
| Script's final program ignores signals | Missing `exec` in the entrypoint script | End the script with `exec "$@"` |
| Zombie processes accumulate | PID 1 does not reap children | Run with `--init` |
Exercise
A container uses CMD node server.js and ignores docker stop for 10 seconds each time. Give two changes that can fix this.
Show answer
Switch to exec form, CMD ["node", "server.js"], so node is PID 1 and receives SIGTERM. Also make sure the program handles SIGTERM, or run the container with --init so an init process forwards the signal.
Exercise
An ENTRYPOINT script ends with node server.js instead of exec node server.js. What happens to SIGTERM sent by docker stop?
Show answer
The shell running the script is PID 1 and does not forward SIGTERM to node. Node never receives it, so Docker waits 10 seconds and sends SIGKILL. Using exec node server.js makes node replace the shell as PID 1.