Skip to content

Instantly share code, notes, and snippets.

@sysboss
Created April 27, 2017 12:47
Show Gist options
  • Select an option

  • Save sysboss/6214f6adf50800d153f47253cc29c4e1 to your computer and use it in GitHub Desktop.

Select an option

Save sysboss/6214f6adf50800d153f47253cc29c4e1 to your computer and use it in GitHub Desktop.

Revisions

  1. sysboss created this gist Apr 27, 2017.
    21 changes: 21 additions & 0 deletions Docker_Images_Cleanup_ByTag.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/bin/bash
    #
    # Keeps latest X tags of every Docker Image

    KEEP_IMAGES_BACK=2

    echo Retrieving local images available

    # get all images
    IMAGES=$(docker images | sort | awk '{print $1}' | uniq | grep -v "<none>")

    echo Cleaning old tags...

    for i in $IMAGES; do
    COUNT=$(docker images | grep "$i " | wc -l)

    if [[ $COUNT -ge 3 ]]; then
    echo IMAGE $i has $COUNT tags
    docker images | grep "$i " | tail -$(($COUNT-$KEEP_IMAGES_BACK)) | awk '{print $1 ":" $2}' | xargs docker rmi
    fi
    done