Is there a way to get tags of a docker image on github container registry (ghcr.io) via curl or gh command?

1.6k Views Asked by At

Is there a way to get tags of a docker image on github container registry via curl or gh command?

curl \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer ${GH_TOKEN}"\
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/orgs/${ORG}/packages/container/${CONTAINER_IMG}

I have tried above but it does not give me docker tags for the iamge.

I am expecting to get the docker tags of ghcr.io containers.

2

There are 2 best solutions below

0
user635332 On

If your environment allows installing software, it is easier to use skopeo. It is available by default on GitHub Action's Linux environment, and in most distros package repos.

skopeo list-tags --registry-token ${GH_TOKEN} docker://ghcr.io/${ORG}/${CONTAINER_IMG}
0
BMitch On

With curl, the commands look like:

#!/bin/sh

repo="${1:-regclient/regctl}"
token=$(curl -s "https://ghcr.io/token?service=ghcr.io&scope=repository:${repo}:pull" \
             -u "${username}:$(cat $HOME/.docker/ghcr_token)" \
        | jq -r '.token')
curl -H "Authorization: Bearer $token" \
     -s "https://ghcr.io/v2/${repo}/tags/list" | jq .

Note that you need to provide a login to the token command (in the -u option). The tags/list API is a standard registry API, not specific to GHCR, but auth will have different hosts for their token server, or just use basic auth without a token.

Tools that simplify registry APIs like this include Google's crane, RedHat's skopeo, or my own regclient.