5
votes

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?

2

2 Answers

7
votes

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
1
votes

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