What is the gcloud API for the Google Container Registry
Asked Answered
H

2

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?

Hahnemann answered 15/3, 2016 at 11:2 Comment(0)
S
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
Sergius answered 15/3, 2016 at 21:22 Comment(6)
You mention "registry API". What exactly do you mean with it? Do you have a link?Hahnemann
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.Sergius
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 <hostname>/<your-project-id>"Misprision
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.Sergius
No worries! Hell, they just changed it again! Here's the new command: "gcloud container images list --repository=<hostname>/<your-project-id>"Misprision
Wow, so awesome to have a working bash script! Good stuff!Lazuli
D
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

Doorknob answered 9/3, 2019 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.