Sub topic 5
Stopping containers
This chapter assumes Docker is installed and the daemon is running. It also assumes you can tell an image from a container and that you know a container's name or ID, which docker ps shows. Here you will stop running containers, see what state they end up in, and learn the common mistakes.
What stopping does
docker stop moves a container from the running state to the exited state. The container is not deleted. Its writable layer, its name, and its configuration stay on disk until you remove it. You can start it again later.
Start a container to practice on:
docker run -d --name web nginx:alpine
docker psNow stop it by name:
docker stop webDocker prints the name you gave it. The container no longer appears in docker ps, but it still exists:
docker ps -aThe STATUS column shows something like Exited (0) 5 seconds ago. The number in parentheses is the exit code of the main process. Code 0 means a clean exit. Code 137 means the process was killed by SIGKILL, which usually means the grace period ran out (see below).
Stopping several containers
docker stop accepts any number of names or IDs. You can also use a unique prefix of an ID.
docker run -d --name web2 nginx:alpine
docker stop web web2To stop every running container, pass the quiet list of IDs from docker ps -q:
docker stop $(docker ps -q)If no containers are running, docker ps -q prints nothing and docker stop fails with a usage error. That is harmless, but it can surprise you in scripts.
How stop works
docker stop is a graceful shutdown. It happens in two steps:
- Docker sends SIGTERM to the container's main process (PID 1). This is a polite request to exit. Well-behaved programs finish current work, close connections, and exit.
- Docker waits for the grace period. The default is 10 seconds. If the process is still alive after that, Docker sends SIGKILL, which ends the process immediately and cannot be caught.
You can change the grace period with -t (or --time):
docker stop -t 30 webThis gives the process up to 30 seconds. Docker does not wait the full time. It returns as soon as the process exits. Use a longer value for databases or queue workers that need time to flush data. Use -t 0 to skip the wait and kill at once.
To skip the polite step entirely, use docker kill:
docker kill webdocker kill sends SIGKILL by default. The process gets no chance to clean up. You can send a different signal with -s, for example docker kill -s HUP web.
| Command | Signal sent | Waits before SIGKILL | Typical use |
|---|---|---|---|
| `docker stop` | SIGTERM, then SIGKILL | 10 seconds by default | Normal shutdown |
| `docker stop -t 30` | SIGTERM, then SIGKILL | 30 seconds | Slow shutdowns |
| `docker kill` | SIGKILL | No wait | Hung or unresponsive containers |
| `docker kill -s HUP` | SIGHUP | Not applicable | Ask a program to reload |
Common errors
Stopping takes exactly 10 seconds every time. The main process is ignoring SIGTERM, so Docker waits out the grace period and then kills it. A frequent cause is a shell-form CMD or ENTRYPOINT in a Dockerfile, such as CMD node app.js. Docker runs that through /bin/sh -c, and the shell becomes PID 1 and does not pass the signal on. Use the exec form, CMD ["node", "app.js"], so your program receives SIGTERM directly. Another cause is a program that does not handle SIGTERM when it runs as PID 1. The --init option on docker run adds a small init process that handles this.
Exit code 137 after stopping. The container was killed by SIGKILL. Either you used docker kill, or the program did not exit within the grace period. Raise the -t value, or fix the signal handling.
No such container. The name or ID is wrong, or the container was already removed. Check with docker ps -a.
Stopping an already stopped container. This does nothing and still prints the name. It is not an error.
The container comes back after you stop it. A restart policy such as --restart always can start containers again after the daemon restarts. A container you stopped by hand with docker stop stays stopped until the daemon restarts or you start it yourself.
Exercise
You run docker stop web and it always takes about 10 seconds. Afterward, docker ps -a shows Exited (137). What is happening, and how would you fix it?
Show answer
The main process ignores SIGTERM, so Docker waits the 10-second grace period and then sends SIGKILL, which gives exit code 137. The usual cause is a shell-form CMD or ENTRYPOINT that leaves a shell as PID 1. Switch to the exec form (for example CMD ["nginx", "-g", "daemon off;"]), make the program handle SIGTERM, or run with --init. A longer -t value only helps if the program really needs more time to shut down.
Exercise
Which command gives a container up to 30 seconds to shut down cleanly before it is force-killed?
Show answer
docker stop -t 30 web