List private Docker repos on Docker Hub from command line (with access token)

778 views Asked by At

Is it possible to list, or search, private Docker repositories on the online version of Docker Hub, from command line, if you've logged in with an access token? I assume curl commands won't work because you can't log in to the web interface with an access token (and this has been my experience trying various curl commands on Docker), can you use the web API somehow?

I have tried various curl and API based solutions (e.g. How to get a list of images on docker registry v2) but they're mostly geared at private registries, not private repos on Docker Hub

1

There are 1 answers

0
BMitch On

Ideally I'd like to be able to list and/or search for repositories within that user (i.e. public and private, assuming I have an access token that allows me to pull those private repos)

This requires custom tooling just for Docker Hub, which exists as the experimental hub-tool. It's not built into other tools because the repository listing API in registries isn't limited to a user or organization. And the API that queries for all repositories is disabled on most SaaS registries because of scaling, privacy, and security reasons. With hub-tool, the command for that looks like:

$ hub-tool repo ls $user_or_org --format json

as well as tags within each repo

This is a lot easier. It's available from registry APIs, and Hub specific APIs. With hub-tool, that's:

$ hub-tool tag ls $org/$repo --format json

(In each of these, I'm using json formatting assuming you want to script the output.)

Other tools work on any registry, including Google's crane, RedHat's skopeo, and my own regclient.

$ crane ls $repo
$ skopeo list-tags docker://$reppo
$ regctl tag ls $repo

Each of these has different ways to login with your PAT.

$ hub-tool login
$ crane auth login $registry
$ skopeo login $registry
$ regctl registry login $registry

With curl, you need to login to get a token using your username and PAT there. This is all Hub specific, so I'd recommend using one of the above tools instead of curl for portability to other registries:

$ cat ./manifest-v2-auth.sh
#!/bin/sh

ref="${1:-library/ubuntu:latest}"
sha="${ref#*@}"
if [ "$sha" = "$ref" ]; then
  sha=""
fi
wosha="${ref%%@*}"
repo="${wosha%:*}"
tag="${wosha##*:}"
if [ "$tag" = "$wosha" ]; then
  tag="latest"
fi
token=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${repo}:pull" \
             -u "$username:$user_pat" \
        | jq -r '.token')
curl -H "Authorization: Bearer $token" \
     -s "https://registry-1.docker.io/v2/${repo}/tags/list" | jq .