Docker Registry API v2を利用してtag、manifest、imageの情報を取得する
APIを利用してDockerのイメージの情報を収集することが可能です。 エントリーポイントによっては認証をする必要があります。
jq
(https://stedolan.github.io/jq/)をインストールしておくとスムーズです。
jq
はShell上でJSONをパースするコマンドツール独自にホスティングしている場合は適宜変更すると良いでしょう。
ここではhttps://hub.docker.com/v2
をエントリーポイントとします。
あらかじめ環境変数にセットしておきます。
export DOCKER_USERNAME="" export DOCKER_PASSWORD=""
# bash TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token) # fish set TOKEN (curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'$DOCKER_USERNAME'", "password": "'$DOCKER_PASSWORD'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
# bash curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/namespaces/ | jq -r '.namespaces|.[]' # fish curl -s -H "Authorization: JWT "$TOKEN"" https://hub.docker.com/v2/repositories/namespaces/ | jq -r '.namespaces|.[]'
# bash USERNAME=library curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${USERNAME}/?page_size=10000 | jq -r '.results|.[]|.name' # fish set USERNAME library curl -s -H "Authorization: JWT "$TOKEN"" "https://hub.docker.com/v2/repositories/$USERNAME/?page_size=10000" | jq -r '.results|.[]|.name'
# bash USERNAME=library REPONAME=node echo "https://hub.docker.com/v2/repositories/${USERNAME}/${REPONAME}/tags/?page_size=10000" curl -s -H "Authorization: JWT ${TOKEN}" https://hub.docker.com/v2/repositories/${USERNAME}/${REPONAME}/tags/?page_size=10000 # fish set USERNAME library set REPONAME node echo "https://hub.docker.com/v2/repositories/$USERNAME/$REPONAME/tags/?page_size=10000" curl -s -H "Authorization: JWT "$TOKEN"" "https://hub.docker.com/v2/repositories/$USERNAME/$REPONAME/tags/?page_size=10000"
# bash USERNAME=library REPONAME=node TAG=latest # 認可情報の取得 TARGET_TOKEN=$(curl \ --silent \ "https://auth.docker.io/token?scope=repository:${USERNAME}/${REPONAME}:pull&service=registry.docker.io" \ | jq -r '.token') curl \ --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \ --header "Authorization: Bearer ${TARGET_TOKEN}" \ "https://registry-1.docker.io/v2/${USERNAME}/${REPONAME}/manifests/${TAG}" \ | jq -r '.config.digest'
# fish set USERNAME library set REPONAME node set TAG latest # 認可情報の取得 set TARGET_TOKEN (curl \ --silent \ "https://auth.docker.io/token?scope=repository:$USERNAME/$REPONAME:pull&service=registry.docker.io" \ | jq -r '.token') curl \ --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \ --header "Authorization: Bearer $TARGET_TOKEN" \ "https://registry-1.docker.io/v2/$USERNAME/$REPONAME/manifests/$TAG" \ | jq -r '.config.digest'
curl
のサンプルコードが書いてあるcurl
のサンプルコードが書いてある