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

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:

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.

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

If you’ve any feedback, feel free to tell me what I can improve, or provide better examples.

Follow me on Twitter and GitHub for more code snippets, posts, updates. Happy coding! :)

My YouTube Channel: https://youtube.com/CoderVlogger

Thanks!

Software Engineer. Go and Python. Learning Rust. https://kenanbek.github.io/

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