Skip to content

Instantly share code, notes, and snippets.

@efrecon
Last active February 8, 2024 13:19
Show Gist options
  • Save efrecon/f99172d827b0a9ff7b6dacb41e4723f8 to your computer and use it in GitHub Desktop.
Save efrecon/f99172d827b0a9ff7b6dacb41e4723f8 to your computer and use it in GitHub Desktop.

Revisions

  1. efrecon revised this gist May 27, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docker_tags.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Docker Tags

    :no_entry: [DEPRECATED] Active as a real [project] instead, development as a
    :no_entry: **[DEPRECATED]** Active as a real [project] instead, development as a
    gist has been discontinued.

    [project]: https://github.com/efrecon/reg-tags
  2. efrecon revised this gist May 27, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion docker_tags.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Docker Tags

    **NOTE** This gist has moved to a real [project] instead, development as a
    :no_entry: [DEPRECATED] Active as a real [project] instead, development as a
    gist has been discontinued.

    [project]: https://github.com/efrecon/reg-tags
  3. efrecon renamed this gist May 27, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions docker_tags.md → docker_tags.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    # Docker Tags

    **NOTE** This gist has moved to a real [project] instead, development as a
    gist has been discontinued.

    [project]: https://github.com/efrecon/reg-tags

    This "script" implements a single function to list the existing tags at the
    Docker [hub] for a given public image.

  4. efrecon revised this gist May 25, 2020. 1 changed file with 29 additions and 14 deletions.
    43 changes: 29 additions & 14 deletions docker_tags.sh
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,17 @@
    #!/usr/bin/env sh

    docker_tags() {
    filter=.*
    _filter=".*"
    _verbose=0
    while [ $# -gt 0 ]; do
    case "$1" in
    -f | --filter)
    filter=$2; shift 2;;
    _filter=$2; shift 2;;
    --filter=*)
    filter="${1#*=}"; shift 1;;
    _filter="${1#*=}"; shift 1;;

    -v | --verbose)
    _verbose=1; shift;;

    --)
    shift; break;;
    @@ -18,42 +22,53 @@ docker_tags() {
    esac
    done


    if printf %s\\n "$1" | grep -oq '/'; then
    hub="https://registry.hub.docker.com/v2/repositories/$1/tags/"
    else
    hub="https://registry.hub.docker.com/v2/repositories/library/$1/tags/"
    fi

    # Get number of pages
    # Decide how to download silently
    download=
    if command -v curl >/dev/null; then
    [ "$_verbose" = "1" ] && echo "Using curl for downloads" >&2
    # shellcheck disable=SC2037
    download="curl -sSL"
    elif command -v wget >/dev/null; then
    [ "$_verbose" = "1" ] && echo "Using wget for downloads" >&2
    # shellcheck disable=SC2037
    download="wget -q -O -"
    else
    return 1
    fi

    # Library images or user/org images?
    if printf %s\\n "$1" | grep -oq '/'; then
    hub="https://registry.hub.docker.com/v2/repositories/$1/tags/"
    else
    hub="https://registry.hub.docker.com/v2/repositories/library/$1/tags/"
    fi
    [ "$_verbose" = "1" ] && echo "Discovering pagination from $hub" >&2

    # Get number of pages
    first=$($download "$hub")
    count=$(printf %s\\n "$first" | sed -E 's/\{\s*"count":\s*([0-9]+).*/\1/')
    [ "$count" = "0" ] && return 0
    if [ "$count" = "0" ]; then
    [ "$_verbose" = "1" ] && echo "No tags, probably non-existing repo" >&2
    return 0
    else
    [ "$_verbose" = "1" ] && echo "$count existing tag(s) for $1" >&2
    fi
    pagination=$( printf %s\\n "$first" |
    grep -Eo '"name":\s*"[a-zA-Z0-9_.-]+"' |
    wc -l)
    pages=$(( count / pagination + 1))
    [ "$_verbose" = "1" ] && echo "$pages pages to download for $1" >&2

    # Get all tags one page after the other
    i=0
    while [ "$i" -le "$pages" ]; do
    while [ "$i" -lt "$pages" ]; do
    i=$(( i + 1 ))
    [ "$_verbose" = "1" ] && echo "Downloading page $i / $pages" >&2
    page=$($download "$hub?page=$i")
    printf %s\\n "$page" |
    grep -Eo '"name":\s*"[a-zA-Z0-9_.-]+"' |
    sed -E 's/"name":\s*"([a-zA-Z0-9_.-]+)"/\1/' |
    grep -E "$filter"
    grep -E "$_filter"
    done
    }

  5. efrecon revised this gist May 24, 2020. 1 changed file with 34 additions and 0 deletions.
    34 changes: 34 additions & 0 deletions docker_tags.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # Docker Tags

    This "script" implements a single function to list the existing tags at the
    Docker [hub] for a given public image.

    [hub]: https://hub.docker.com/

    When called as a script, all arguments will be forwarded to the function so that
    you will be able to try it.

    ## Synopsis

    The function takes short options led by a single-dash, or long options led by a
    double dash. Long options can be separated from their value by an equal sign or
    a space separator. Recognised options are:

    + `-f` or `--filter`, a regular expression to restrict tags to versions matching
    the expression.

    ## Example

    The following will return all tags for the official [alpine] image:

    ```shell
    ./docker_tags.sh alpine
    ```

    The following would only return "real" releases for [alpine]:

    ```shell
    ./docker_tags.sh --filter '[0-9]+(\.[0-9]+)+' alpine
    ```

    [alpine]: https://hub.docker.com/_/alpine
  6. efrecon revised this gist May 24, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion docker_tags.sh
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -57,6 +57,7 @@ docker_tags() {
    done
    }

    if [ "$#" -gt "0" ]; then
    # Run directly? call function!
    if [ "$(basename "$0")" = "docker_tags.sh" ] && [ "$#" -gt "0" ]; then
    docker_tags "$@"
    fi
  7. efrecon created this gist May 24, 2020.
    62 changes: 62 additions & 0 deletions docker_tags.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    #!/usr/bin/env sh

    docker_tags() {
    filter=.*
    while [ $# -gt 0 ]; do
    case "$1" in
    -f | --filter)
    filter=$2; shift 2;;
    --filter=*)
    filter="${1#*=}"; shift 1;;

    --)
    shift; break;;
    -*)
    echo "$1 unknown option!" >&2; return 1;;
    *)
    break;
    esac
    done


    if printf %s\\n "$1" | grep -oq '/'; then
    hub="https://registry.hub.docker.com/v2/repositories/$1/tags/"
    else
    hub="https://registry.hub.docker.com/v2/repositories/library/$1/tags/"
    fi

    # Get number of pages
    download=
    if command -v curl >/dev/null; then
    # shellcheck disable=SC2037
    download="curl -sSL"
    elif command -v wget >/dev/null; then
    # shellcheck disable=SC2037
    download="wget -q -O -"
    else
    return 1
    fi

    first=$($download "$hub")
    count=$(printf %s\\n "$first" | sed -E 's/\{\s*"count":\s*([0-9]+).*/\1/')
    [ "$count" = "0" ] && return 0
    pagination=$( printf %s\\n "$first" |
    grep -Eo '"name":\s*"[a-zA-Z0-9_.-]+"' |
    wc -l)
    pages=$(( count / pagination + 1))

    # Get all tags one page after the other
    i=0
    while [ "$i" -le "$pages" ]; do
    i=$(( i + 1 ))
    page=$($download "$hub?page=$i")
    printf %s\\n "$page" |
    grep -Eo '"name":\s*"[a-zA-Z0-9_.-]+"' |
    sed -E 's/"name":\s*"([a-zA-Z0-9_.-]+)"/\1/' |
    grep -E "$filter"
    done
    }

    if [ "$#" -gt "0" ]; then
    docker_tags "$@"
    fi