diff options
-rwxr-xr-x | tcwg-cleanup-stale-containers.sh | 35 |
1 files changed, 12 insertions, 23 deletions
diff --git a/tcwg-cleanup-stale-containers.sh b/tcwg-cleanup-stale-containers.sh index d5d999d9..18710eb8 100755 --- a/tcwg-cleanup-stale-containers.sh +++ b/tcwg-cleanup-stale-containers.sh @@ -174,29 +174,18 @@ if [ ${#rm_volumes[@]} != 0 ]; then fi fi -rm_images=() -for image in $($DOCKER images -q -f dangling=true); do - if ! $DOCKER ps -a --format "{{.Image}}" | grep -q $image; then - rm_images=("${rm_images[@]}" $image) - fi -done - -if [ ${#rm_images[@]} != 0 ]; then - echo "Removing unused images" - if $cleanup_images; then - for image in "${rm_images[@]}"; do - $DOCKER rmi $image & - res=0; wait $! || res=$? - if [ $res != 0 ]; then - echo "WARNING: $DOCKER rmi $image -- exit status: $res" - status=$(($status|8)) - fi - done - else - echo "Increasing exit code to indicate unused images" - status=$(($status|8)) - echo "DRY_RUN: NOT REMOVING UNTAGGED IMAGES" - fi +if $cleanup_images; then + for image in $($DOCKER images -q -f dangling=true); do + # Try to remove $image. The command will fail if the image is + # used by something. + $DOCKER rmi $image & + res=0; wait $! || res=$? + if [ $res = 0 ]; then + echo "Removed image $image" + fi + done +else + echo "DRY_RUN: NOT REMOVING UNTAGGED IMAGES" fi # This is a workaround for https://issues.jenkins-ci.org/browse/JENKINS-49097. |