DEV Community

Andrei Dascalu
Andrei Dascalu

Posted on

3 2

Docker cleanup: images by name search

There's nothing more annoying than collecting docker images locally and suddenly stateful application containers (like databases, mySQL, RabbitMQ) suddenly exiting because they don't have space left on the (usually VM) where they are running.

You can docker system prune -a to clean as much as possible or you can docker rmi everything, but what if you want to do a bit more targeted cleaning?

docker image ls will list all images with some info split in columns. First column is the image name, the second column contains the tag.

Now, we know that images share layers so deleting one won't necessarily free up all the space since some layers may still be linked to other images but we can improve cleanup by untagging images. How to do that in a more targeted way? By listing them and then joining the name column and the tag column and then passing it all to docker rmi. AWK comes to the rescue!

docker image ls | grep "MY SEARCH" | awk '{print $1 ":" $2}' | xargs docker rmi
Enter fullscreen mode Exit fullscreen mode

Of course, feel free to replace grep with your preferred search command as long as its output only filters docker image ls output and doesn't extract information from it, as AWK expects the columnised output.

Note: you should still do a

docker system prune
Enter fullscreen mode Exit fullscreen mode

afterwards as unlinking tags only frees up layers but those layers may remain stored locally - but docker prune will remove the dangling ones (eg: not tied to tagged images or to running containers).

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay