🎓 A1Learner
🔍
0 of 50 lessons completed

Clean Up with Docker Prune

Docker keeps stopped containers, unused images and old networks until they are removed. docker prune deletes the unused ones and reports how much disk it reclaimed.

Remove stopped containers

This command removes every stopped container.

docker container prune -f | grep "Total reclaimed space"

Remove one image by name

This command removes a single image by repository and tag.

docker rmi hello-world:latest

Example

docker run hello-world >/dev/null
docker container prune -f | grep "Total reclaimed space"
docker ps -a --format "{{.Names}} {{.Image}}"

Key points

  • Use docker container prune to remove stopped containers.
  • Use docker image prune to remove unused images.
  • Use docker system prune to remove stopped containers, unused images and networks at once.
  • Add -a to remove all unused images, not just dangling ones.
  • Add -f to skip the confirmation question.
  • Use docker rm and docker rmi to remove one container or image by name.

In this example, Docker runs hello-world and discards its greeting. docker container prune then removes the stopped container and reports the space it reclaimed. The last listing is empty, which proves the container is gone.

Note

Prune never touches running containers or images they use.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What does docker system prune remove?