diff options
Diffstat (limited to 'tcwg-cleanup-stale-containers.sh')
-rwxr-xr-x | tcwg-cleanup-stale-containers.sh | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/tcwg-cleanup-stale-containers.sh b/tcwg-cleanup-stale-containers.sh index 2fa246cf..fe626621 100755 --- a/tcwg-cleanup-stale-containers.sh +++ b/tcwg-cleanup-stale-containers.sh @@ -176,7 +176,22 @@ else fi if $cleanup_images; then - docker image prune -a -f + # See start-container-docker.sh for background on image stamp files. + stamp_dir=/home/shared/docker + + # Untag and prune images that haven't been used for 3 days or more. + for image in $(docker images --format "{{.Repository}}:{{.Tag}}"); do + image_stamp="$stamp_dir/$(echo "$image" | tr "/:" "_")" + remove_if_not_used_since=$(($(date +%s) - 3*24*60*60)) + # Use negative comparison to handle non-existent stamp files. + if ! [ "$(stat -c %Z "$image_stamp.use" 2>/dev/null)" \ + -gt $remove_if_not_used_since ]; then + # Untag the image. + docker rmi "$image" + fi + done + # Prune untagged images. + docker image prune -f else echo "DRY_RUN: NOT REMOVING UNUSED IMAGES" fi |