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.