Skip to content

La vie rêvée d’Akim

…ou le zèbre qui voulait être un homme

  • Style de vie
  • Informatique
  • Photographie
  • Musique
  • English (US)English (US)
  • FrançaisFrançais

One Liner: Delete all docker images in bash

Posted on Thursday 2 February 2023 - Thursday 2 February 2023 by Akim

Easy and simple dirty one liner to delete all images in docker. Useful in a lab setup, when you want to clean images. And good reference to learn how to make a loop out of a command output

for image in $(while read image;do echo $image | grep ago | cut -d" " -f3;done < <(sudo docker images));do sudo docker image rm $image;done

Let's clean it up and read it together, shall we ?

for image in 
    $(
        while read image;
        do 
            echo $image | grep ago | cut -d" " -f3;
        done 
    < <(sudo docker images))
do 
    sudo docker image rm $image
done

That clarifies a bit. So let's describe the actions.

For each occurrence of variable image, execute docker image rm to delete that image. So far, quite clear

for image in
...
do
   sudo docker image rm $image
done

Basically, for each image we find in a given command (we will see below), delete that image with docker image rm command. So "in" what shall we find the image IDs ?

    $(
        while read image;
        do 
            echo $image | grep ago | cut -d" " -f3;
        done 
    < <(sudo docker images))

This can be red as this: For each line you find in "sudo docker images", include only lines which do contain "ago" (because this allow to exclude the header of the result, as each image has a time stamp finishing by "ago" for when it was created). Then, print the third field of the line, using "space" as a separator.

At that stage, this will return a list of image IDs. And this is the value that will be used by the previous line to run docker image rm.

That's it. Run this one liner, and all docker images will be deleted, as long as they do not feed a running container.

Posted in Informatique

Post navigation

Python: Download ZIP and parse URL from text file

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Proudly powered by WordPress | Theme: micro, developed by DevriX.