Skip to content

Instantly share code, notes, and snippets.

@codepunkt
Created December 1, 2023 07:37

Revisions

  1. codepunkt created this gist Dec 1, 2023.
    25 changes: 25 additions & 0 deletions script.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # to be pasted into .bashrc or .zshrc
    docker_image_platform() {
    if [ -z "$1" ]; then
    echo "Error: Please provide a Docker image name with version."
    return 1
    fi

    local image="$1"
    local version=$(docker --version | awk '{print $3}' | tr -d ',')

    if ! command -v jq &> /dev/null; then
    echo "Error: jq is not installed."
    return 1
    fi

    local url="http://v${version}/distribution/${image}/json"
    local response=$(curl --unix-socket /var/run/docker.sock -s -o /dev/null -w "%{http_code}" "$url")

    if [ "$response" -ge 200 ] && [ "$response" -lt 300 ]; then
    curl -sS --unix-socket /var/run/docker.sock "$url" | jq '.Platforms'
    else
    echo "Error: Unable to retrieve information for $image."
    return 1
    fi
    }