Sub topic 12
Troubleshooting: containers that will not stop or remove
This chapter assumes you can list, stop, start, and remove containers with docker ps, docker stop, docker start, and docker rm. It also assumes you know what SIGTERM and SIGKILL are. Here you will diagnose the most common cases where a container refuses to stop or be removed, and you will learn a fix for each.
Start with the state
Before you change anything, check what Docker thinks the container is doing.
docker ps -a --filter "name=web" --format "table {{.Names}}\t{{.Status}}\t{{.Image}}"
docker inspect --format "{{.State.Status}} pid={{.State.Pid}} exit={{.State.ExitCode}}" webThe first command shows the status column. The second prints the state, the host process ID of the container's main process, and the last exit code. A PID of 0 means no process is running.
Problem 1: `docker stop` takes about ten seconds
docker stop sends SIGTERM and waits for the grace period, 10 seconds by default. If the main process has not exited by then, Docker sends SIGKILL. A stop that always takes about 10 seconds means your process ignores SIGTERM.
The usual cause is that the process runs as PID 1 inside the container. Linux gives PID 1 no default signal handlers, so a program that does not install its own SIGTERM handler will ignore the signal. Another cause is a shell-form command such as CMD node server.js. This runs your program under /bin/sh -c, and the shell may not pass the signal on.
Fixes, from the most lasting to the quickest:
- Use the exec form in the Dockerfile:
CMD ["node", "server.js"]. - Handle SIGTERM in your application code.
- Start the container with
docker run --initso a small init process forwards signals. - Use
docker kill webwhen you need it gone now.
docker run -d --init --name web nginx:alpine
time docker stop webProblem 2: `docker rm` refuses to remove a container
The usual message is an error saying you cannot remove a running container. Stop it first, or force removal.
docker stop web
docker rm webdocker rm -f web does both steps by sending SIGKILL and then removing the container. It skips the graceful shutdown, so use it only when losing in-flight work is acceptable.
Problem 3: "No such container"
This error means the name or ID you typed does not match any container. Check these causes in order:
- A typo in the name, or an ID prefix that is too short to be unique.
- The container was already removed, possibly by
--rmor a prune. - You are talking to a different Docker daemon, for example through a changed
DOCKER_HOSTor a different context. Rundocker context lsto check.
Run docker ps -a to see every container the current daemon knows about. A stopped container only appears with -a.
Problem 4: the container comes back after you stop it
If you started the container with --restart always or --restart unless-stopped, Docker restarts it when it exits. A manual docker stop is respected by these policies. The container can still return after a daemon restart with always, or after a crash loop with on-failure. Check the policy:
docker inspect --format "{{.HostConfig.RestartPolicy.Name}}" web
docker update --restart=no web
docker stop webProblem 5: the container is stuck and even `kill` does nothing
This is rare. If a process is blocked in the kernel, for example waiting on an unresponsive network file system or disk, it cannot act on SIGKILL until that call returns. Look at the state with docker inspect, then check the host for I/O problems. Restarting the Docker daemon is a last resort, because it can affect every container on the host.
Summary of fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| `stop` takes about 10 seconds | Process ignores SIGTERM | Exec-form `CMD`, signal handler, or `--init` |
| `rm` says container is running | Container still running | `docker stop` then `docker rm`, or `docker rm -f` |
| No such container | Typo, already removed, wrong daemon | `docker ps -a`, `docker context ls` |
| Container returns | Restart policy | `docker update --restart=no` |
| `kill` has no effect | Process blocked in the kernel | Fix the host I/O problem |
Exercise
Running docker stop api always takes almost exactly 10 seconds, and docker inspect shows exit code 137 afterward. What does this tell you, and what are two ways to fix it?
Show answer
Exit code 137 is 128 + 9, which means the process was killed by SIGKILL. The process ignored SIGTERM, so Docker waited out the 10-second grace period and then killed it. Fixes: use the exec form of CMD in the Dockerfile or add a SIGTERM handler to the application, or start the container with docker run --init so an init process forwards signals.
Exercise
You run docker rm web and get an error that the container is running. Name two ways to remove it, and say which one skips graceful shutdown.
Show answer
Run docker stop web and then docker rm web, or run docker rm -f web. The -f form skips graceful shutdown because it sends SIGKILL immediately.