golang approach (tested after running: gcloud auth configure-docker
):
package main
import (
"context"
"fmt"
"log"
"github.com/google/go-containerregistry/pkg/authn"
gcr "github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
func main() {
auth, err := google.NewGcloudAuthenticator()
if err != nil {
log.Fatal(err)
}
fmt.Println(auth)
registry, err := gcr.NewRegistry("gcr.io")
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
repos, err := remote.Catalog(ctx, registry, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
log.Fatal(err)
}
for _, repo := range repos {
fmt.Println(repo)
}
}
response:
&{0xc0000000}
project-id/imagename
REST API Approach:
you can list the images like this (replace the gcr.io with your gcr endpoint such as gcr.io, us.gcr.io, asia.gcr.io etc.):
curl -H "Authorization: Bearer $(gcloud auth print-access-token)" "https://gcr.io/v2/_catalog"
response:
{"repositories":["project-id/imagename"]}
you can find some related details regarding the token fetching from this link:
How to list images and tags from the gcr.io Docker Registry using the HTTP API?
PS: you will have to filter out the project specific images from response if you have access to multiple projects