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 -a

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

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)

Like what you read? Give Kanan Rahimov a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.