Last active
January 4, 2024 13:40
-
-
Save putnamhill/81e373839350c362683b to your computer and use it in GitHub Desktop.
A script to help clean up docker images interactively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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