Sub topic 11
Stopping, removing, and cleaning up
This chapter assumes you can start containers with docker run, list them with docker ps, and that you know what a named volume is. It covers how to end a container's life cleanly and how to reclaim the disk space it leaves behind.
Stopping a container
docker stop asks a container to exit. Docker sends SIGTERM to the main process (PID 1). If the process is still running after a grace period of 10 seconds, Docker sends SIGKILL.
docker stop web
docker stop -t 30 dbThe -t option changes the grace period. Databases often need longer than 10 seconds to flush data to disk.
docker kill sends SIGKILL right away and gives the process no chance to clean up. Use it only for a container that ignores docker stop.
A stopped container is not gone. Its filesystem changes, its logs, and its configuration remain on disk. You can start it again with docker start web.
Removing a container
docker rm deletes a stopped container along with its writable layer.
docker rm web
docker rm -f web-f stops a running container and removes it in one step. It uses SIGKILL, so prefer docker stop followed by docker rm when the application needs a clean shutdown.
If you only need a short-lived container, add --rm to docker run. Docker removes the container as soon as it exits.
What removal leaves behind
Removing a container does not remove everything it used.
| Resource | Removed by `docker rm`? | How to remove it |
|---|---|---|
| Container and its writable layer | Yes | `docker rm` |
| Image | No | `docker rmi` or `docker image prune` |
| Named volume | No | `docker volume rm` or `docker volume prune` |
| Anonymous volume | Only with `-v` | `docker rm -v` |
| User-defined network | No | `docker network rm` or `docker network prune` |
| Bind-mounted host directory | Never | You delete it yourself |
Cleaning up in bulk
Prune commands remove everything of one kind that is not in use. Each asks for confirmation.
docker container prune
docker volume prune
docker network prune
docker image prunedocker container prune removes all stopped containers. docker image prune removes only dangling images, which are untagged layers left over after a rebuild. Add -a to remove every image that no container uses.
docker system prune runs several of these together. It removes stopped containers, unused networks, dangling images, and build cache. It does not remove volumes unless you add --volumes.
To see how much space Docker uses before you delete anything:
docker system dfA typical cleanup sequence
Applied to the database and network from earlier chapters:
docker stop db cache
docker rm db cache
docker volume rm pgdata
docker network rm appnetCommon errors
container is running:docker rmrefuses to delete a running container. Stop it first or use-f.volume is in use: a container, even a stopped one, still references the volume. Remove that container first.network has active endpoints: a container is still attached to the network. Remove or disconnect it first.image is being used by stopped container: remove the container before the image.
Exercise
You run docker rm db on a PostgreSQL container started with -v pgdata:/var/lib/postgresql/data. A week later you start a new container with the same volume mount. Is the data still there, and why?
Show answer
Yes. docker rm removes the container and its writable layer but not named volumes. The pgdata volume remains until you run docker volume rm pgdata or docker volume prune, so the new container finds the existing data.
Exercise
Which command removes every stopped container, and which extra flag would docker system prune need in order to also delete unused volumes?
Show answer
docker container prune removes all stopped containers. docker system prune --volumes also removes unused volumes.