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).

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

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 !! :)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

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

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay