5

I have to list the Docker container images published in a certain project, but I cannot find an appropriate API using the gcloud CLI tool. Is this possible?

Is there any other solution to list the container images form this private container registry in my Google project?

Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168

2 Answers2

7

You can use "gcloud docker search <hostname>/<your-project-id>" to list the images. Hostname should be "gcr.io", or "us.gcr.io" or whatever your images are created under. Please note you have to iterate through all possible hosts to find all images under the project. However, this method only list the repositories, it will not list tags or manifests.

You can also use registry API directly to do that and it will return more information. Using the below script as a starting guide:

#!/bin/bash

HOSTS="gcr.io us.gcr.io eu.gcr.io asia.gcr.io"
PROJECT=your-project-id

function search_gcr() {
  local fullpath=""
  local host=$1
  local project=$2
  if [[ -n $3 ]]; then
    fullpath=${3}
  fi
  local result=$(curl -u _token:$(gcloud auth print-access-token) \
    --fail --silent --show-error \
    https://${host}/v2/${project}${fullpath}/tags/list)
  if [[ -z $result ]]; then
    printf ""
  else
    printf $result
  fi
}

function recursive_search_gcr() {
  local host=$1
  local project=$2
  local repository=$3
  local result=$(search_gcr $host $project ${repository})
  local returnVal=$?
  if [[ -z $result ]]; then
    echo Not able to curl: https://${host}/v2/${project}${fullpath}/tags/list
    return
  fi
  local children="$(python - <<EOF
import json
import sys
obj = json.loads('$result')
if 'child' in obj:
  print ' '.join(obj['child'])
else:
  print ''
EOF
    )"

  for child in $children;
  do
    recursive_search_gcr $host $project ${repository}/${child}
  done
  local manifests="$(python - <<EOF
import json
import sys
obj = json.loads('$result')
if 'manifest' in obj:
  print ' '.join(obj['manifest'])
else:
  print ''
EOF
    )"
  echo Repository ${host}/${project}$repository:
  echo "    manifests:"
    for manifest in $manifests
    do
      echo "        "$manifest
    done
    echo

  local tags="$(python - <<EOF
import json
import sys
obj = json.loads('$result')
if 'tags' in obj:
  print ' '.join(obj['tags'])
else:
  print ''
EOF
    )"
  echo "    tags:"
  for tag in $tags
  do
    echo "        "$tag
  done
  echo
}

for HOST in $HOSTS;
do
  recursive_search_gcr $HOST $PROJECT
done
Wei
  • 309
  • 2
  • 2
  • You mention "registry API". What exactly do you mean with it? Do you have a link? – Gabriel Petrovay Mar 16 '16 at 00:49
  • 3
    https://docs.docker.com/registry/spec/api/. Google Container Registry implements the same rest APIs, but extended the list image tags API so it returns a super set of information of what the above link defines, that's why you can recursively list images. – Wei Mar 16 '16 at 16:33
  • I haven't tried your script and I apologize in advance if this is obvious, but to get the search to work I had to do: "gcloud docker -- search /" – Bryan Cote-Chang Apr 21 '17 at 17:22
  • The "--" between "gcloud docker" and "search" is a recent thing added to gcloud command. It wasn't needed at the time of me posing my first answer. But thank you for bring that up. – Wei Apr 21 '17 at 21:44
  • No worries! Hell, they just changed it again! Here's the new command: "gcloud container images list --repository=/" – Bryan Cote-Chang Jun 19 '17 at 22:58
  • Wow, so awesome to have a working bash script! Good stuff! – mindreframer Jan 17 '19 at 12:44
1

Use the "gcloud container images" command to find and interact with images in Google Container Registry. For example, this would list all containers in a project called "my-project":

gcloud container images list --repository=gcr.io/my-project

Full documentation is at https://cloud.google.com/container-registry/docs/managing