How to remove Docker images and containers

Stop and remove containers first, then images. Prune commands clean up in bulk, so read what they will delete.

Remove a container

docker ps -a
docker stop <container>
docker rm <container>

Use docker rm -f <container> to stop and remove a running container in one step.

Remove an image

docker images
docker rmi <image>

Docker refuses to remove an image that a container still uses, so remove that container first.

Clean up in bulk

docker container prune
docker image prune
docker system prune

docker system prune removes stopped containers, unused networks, dangling images and build cache. Add -a to remove all unused images, and --volumes to include volumes.

Warning: Volumes hold data such as databases. They are not removed unless you ask for it with --volumes, and that cannot be undone.

Questions

What is a dangling image?

An image without a tag, usually left behind when you rebuild an image under the same name.

How do I remove everything?

Run docker rm -f $(docker ps -aq) for all containers, then docker rmi $(docker images -q) for all images. Check the lists first.