1

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 ?

Keval Bhogayata
  • 4,422
  • 3
  • 13
  • 36
  • 1
    I see comments in my dashboard suggesting that container registry is deprecated and we should move to artefact registry instead. – Tom Anderson Jan 10 '22 at 14:28

2 Answers2

4

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.

boredabdel
  • 1,732
  • 3
  • 7
  • Thanks @boredabdel, I also referred https://pkg.go.dev/github.com/docker/docker/registry, I am not sure which particular method can help me with repository listing, you got any ideas ? – Keval Bhogayata Jan 11 '22 at 11:36
  • All I have got is a service account json for gcp and I am able to push and pull the images using docker GO sdk – Keval Bhogayata Jan 11 '22 at 11:39
  • I don't think that that's possible, the Docker API doesn't understand the concept of GCP projects and we don't have an API that can allow you to list repositories :( – boredabdel Jan 11 '22 at 12:37
1

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

ClumsyPuffin
  • 3,909
  • 1
  • 17
  • 17