Deleting docker images with pipes

Needed to delete all images from docker, except for some that I'm currently using, in this case named keyword.

The awk part is to complete the image name with it's tag, probably latest or some version like 1.2.3.

docker images | grep -v keyword | awk '{print $1":"$2}' | xargs -n 1 docker rmi

grep -v keyword greps lines that don't match keyword, so everything but that. The awk part is to format the output as image:tag. xargs is to feed the input as arguments to the command docker rmi (or any other command too), in this case I'm making sure that it runs docker rmi for each argument in the input with -n 1 (which is the default, in case I want to run a command with more arguments, I just change that number)

Also, if I want to further filter the list of images, I just append another grep to the pipeline.

docker images | grep filter | grep -v keyword | awk '{print $1":"$2}' | xargs -n 1 docker rmi