Docker cleanup, build and force to rebuild images, containers, volumes and networks

Kanan Rahimov
Apr 5, 2018 · 2 min read

All we know that after few projects and some tests/plays with Docker it downloads and keeps stored all related staffs such as volumes, networks, images. At some point you need to cleanup you local machine and free space. For this purpose you can use following command:

docker system prune -a

It will ask you to confirm removing of containers, volumes, networks and images:

Image for post
Image for post

This command with a given -a option will remove everything from local machine not just dangling images. Please be careful. If you use same images a lot this command will remove them as well. On next build/up Docker will download them again (so additional traffic).

Without -a option it will remove only dangling images:

docker system prune

With Docker Compose it comes really messy because you usually have few containers. If your purpose is just to force Docker to rebuild images without using cache then use following combination:

docker system prune
docker-compose build --no-cache

Also, if you get a message saying that you run out of space, best thing you can do in this case is to remove unnecessary containers and images.

docker rm $(docker ps -q -f 'status=exited')
docker rmi $(docker images -q -f "dangling=true")

Then run up command as usual:

docker-compose up

I hope this commands will keep your local machine a little clean for your daily DevOps issues.

How To Remove Docker Images, Containers, and Volumes (DigitalOcean)
Docker Pruning (official docs)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store