Keval Bhogayata
Keval Bhogayata

Reputation: 6736

How to extract list of docker images from GCP Container Registry?

I want to get list of all the docker images that I have uploaded on a particular project.

I am using the official SDK https://pkg.go.dev/cloud.google.com/go

As per my understanding the listing should be under the container module. But, I am not able to find any method called ListImages OR ListRepositories that can server the purpose.

I checked out the artifactregistry module, but it seems that it is only useful in case I push my images to artifact registry.

What is the correct way to get listing of docker images (project wise), in golang ?

Upvotes: 1

Views: 1704

Answers (2)

ClumsyPuffin
ClumsyPuffin

Reputation: 4069

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

Upvotes: 1

boredabdel
boredabdel

Reputation: 2140

I don't think we have a client library for the containerregistry. Since it's just an implementation of the Docker Registry API according to this thread at least

Have you tried with the Registry package ? https://pkg.go.dev/github.com/docker/docker/registry

For the repository you can use hostname/project-id. Where hostname is gcr.io or eu.gcr.io or us.gcr.io depending on how your repositories in GCR are configured.

Upvotes: 4

Related Questions