You deploy, you rebuild, you test things. And one day, you look at your server and you have only 2 GB of free space left. Congratulations, Docker has eaten it all up.
Your Docker hard drive hates you. And it’s your fault.
What exactly is the problem?
Docker accumulates images over time. Every time you run a docker build, if anything changes, it creates new layers and keeps the old ones. These old layers which have no tags, no names, and no purpose are called dangling images.
They serve no purpose. They take up space. And Docker doesn’t delete them on its own.
See where you stand
Before deleting anything, there’s just one thing to do:
docker system df
Enter fullscreen mode Exit fullscreen mode
This shows you in a few lines how much space is being used—and, more importantly, how much is available. That’s your starting point.
Want to see the images in detail?
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.Size}}"
Enter fullscreen mode Exit fullscreen mode
Delete dangling images
Dangling images are intermediate layers that are left behind. They have neither a tag nor a name. To delete them:
docker image prune
Enter fullscreen mode Exit fullscreen mode
Docker asks for your confirmation, shows you what it’s going to delete, and you confirm. Clean, fast, and risk-free.
Go a step further
If you have entire images that are no longer used by any container (even stopped ones), you can delete them as well:
docker image prune -a
Enter fullscreen mode Exit fullscreen mode
The -a option changes everything: now it no longer targets just dangling images, but all unreferenced images. That’s what you want in most cases on a production or staging server.
You can filter by age if you want to keep recent images:
docker image prune -a --filter "until=720h"
Enter fullscreen mode Exit fullscreen mode
It only deletes items that are more than 30 days old. This is handy if you have regular deployments.
Reset Everything
If you want to reset everything images, stopped containers, orphaned volumes, unused networks:
docker system prune -a --volumes
Enter fullscreen mode Exit fullscreen mode
I don’t recommend doing this on a production server without first checking what’s running. But on a dev machine or a VPS that you rebuild regularly, it’s perfect.
Automating All of This
Instead of doing this manually, you can set up a weekly cron job:
# crontab -e
0 3 * * 0 docker image prune -a -f >> /var/log/docker-prune.log 2>&1
Enter fullscreen mode Exit fullscreen mode
Every Sunday at 3 AM, it cleans up without asking for confirmation (-f), and logs the result.
Conclusion
Command What it doesdocker system df
View used space
docker image prune
Remove dangling images
docker image prune -a
Remove everything that is no longer in use
docker image prune -a --filter “until=720h”
Same as above, but only for old images
docker system prune -a --volumes
Reset everything to zero
Start with docker system df. In 90% of cases, running docker image prune -a is enough to free up several gigabytes without breaking anything.
답글 남기기