Docker is a powerful tool for running applications in isolated "containers". However, over time, it can fill up your disk with unused files like old images, stopped containers, and more. This guide will show you how to clean up Docker safely and effectively.
When you use Docker, it creates and stores various resources. Over time, these can take up a lot of space:
- Stopped Containers: Containers you ran but stopped still take up space.
- Dangling Images: Images without tags or references, often leftover from builds.
- Unused Images: Images not used by any container (running or stopped).
- Unused Volumes: Persistent data storage created by containers you no longer use.
- Unused Networks: Custom networks created for containers that no longer exist.
- Build Cache: Temporary files created during image builds to speed up future builds.
Docker provides simple commands to clean up unused resources. Be careful—some commands permanently delete data. Always double-check before running them.
This is the most common and safest cleanup command. It removes:
- Stopped containers
- Unused networks
- Dangling images
- Build cache
Command:
docker system pruneExample:
$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] yThis removes everything the standard prune does, plus unused images (images not used by any container). Use this only if you're okay with re-downloading images later.
Command:
docker system prune -aExample:
$ docker system prune -a
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all unused images
- all build cache
Are you sure you want to continue? [y/N] yIf you want to clean up only one type of resource, use these commands:
-
Stopped Containers:
docker container prune
(Example: Removes all stopped containers.)
-
Dangling Images:
docker image prune
(Example: Removes images without tags or references.)
-
Unused Volumes: (Be careful—this deletes data!)
docker volume prune
(Example: Removes volumes not attached to any container.)
-
Unused Networks:
docker network prune
(Example: Removes networks not used by any container.)
-
Build Cache:
docker builder prune
(Example: Removes temporary files from image builds.)
Want to see how much space Docker is using before cleaning? Use this command:
Command:
docker system dfExample:
$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 10 3 5.2GB 3.1GB (60%)
Containers 5 1 1.2GB 1.0GB (83%)
Local Volumes 8 2 2.5GB 2.0GB (80%)
Build Cache - - 1.0GB 1.0GB- Start with
docker system prune: It's the safest and easiest way to clean up. - Use
docker system dffirst: Check what’s taking up space before deleting anything. - Be cautious with
-aand volume pruning: These commands can delete data you might need later. - Run cleanup regularly: Keeping Docker tidy helps avoid running out of disk space.
Happy Docker-ing! 🐳✨