Skip to content

Instantly share code, notes, and snippets.

@putnamhill
Last active January 4, 2024 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save putnamhill/81e373839350c362683b to your computer and use it in GitHub Desktop.
Save putnamhill/81e373839350c362683b to your computer and use it in GitHub Desktop.
A script to help clean up docker images interactively
#!/bin/bash
agree() {
local question="$1"
while true; do
read -p "$question " answer
case $answer in
[Yy]|[Yy][Ee][Ss]) return 0 ;;
[Nn]|[Nn][Oo]|'') return 1 ;; # empty string is default
*) echo "Please answer yes or no." ;;
esac
done
}
# Read image fields from file descriptor 3 using process substitution
# This lets us get feedback from the user via stdin within the while read loop
# (skip the header line with sed)
exec 3< <(docker images | sed '1d')
while read -u 3 repo tag image info; do
agree "Remove image tagged $tag from repo $repo ($(echo $info))?" \
&& docker rmi $image
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment