DEV Community

GaMa
GaMa

Posted on • Edited on

3 1

How to remove temporal untagged Docker images

*Update, thanks to champkeh

The correct way to remove unused docker images is using:

docker image prune [OPTIONS]

For more information you can go to:

https://docs.docker.com/engine/reference/commandline/image_prune/


If you have some experience with Docker you will know that mistakes are made while building your own images. At the beginning I didn't cared to delete one untagged image from time to time by using the docker rmi ###### command, however, when I had to work on a project to migrate a legacy application to Docker, I realized that there are a LOT of images just hanging out in the ether like "we are here just chillin".

chillin

So, I started doing some cleanup one by one, until, you know, who wants to paste the image id or the name to delete it? Right. Then I came up with the following one-liner:

docker images | awk '{if(match($1,"<none>")){print "docker rmi " $3 ";" | "/bin/sh"}}'

You can then add the line to a shell script or whatever.The good thing is that if by some reason an image has a dependen child (container associated to it) it won't delete it unless you force it (-f).

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (2)

Collapse
 
champkeh profile image
champ • Edited

why try this:

docker image prune

or

docker image prune -f
Collapse
 
ech0server profile image
GaMa

This is awesome, did not know it existed, thank you I will update my post !! :)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay