如何从GCP容器注册表中提取Docker镜像列表?

huangapple go评论86阅读模式
英文:

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

问题

我想获取我在特定项目上上传的所有Docker镜像的列表。

我正在使用官方的SDK https://pkg.go.dev/cloud.google.com/go

根据我的理解,列表应该在container模块下。但是,我找不到任何名为ListImages或ListRepositories的方法可以实现这个目的。

我查看了artifactregistry模块,但似乎只有在我将镜像推送到artifact registry时才有用。

在golang中,获取Docker镜像(按项目分类)的正确方法是什么?

英文:

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 ?

答案1

得分: 4

我不认为我们有一个用于containerregistry的客户端库。根据这个帖子,它只是根据Docker Registry API的实现。

你尝试过使用Registry包吗?https://pkg.go.dev/github.com/docker/docker/registry

对于仓库,你可以使用hostname/project-id的格式。其中,hostname可以是gcr.io、eu.gcr.io或us.gcr.io,具体取决于你在GCR中的仓库配置。

英文:

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.

答案2

得分: 1

golang方法(在运行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)
	}
}

响应:

&{0xc0000000}
project-id/imagename

REST API方法:

您可以像这样列出镜像(将gcr.io替换为您的gcr端点,例如gcr.io、us.gcr.io、asia.gcr.io等):

curl -H "Authorization: Bearer $(gcloud auth print-access-token)"   "https://gcr.io/v2/_catalog"

响应:

{"repositories":["project-id/imagename"]}

您可以在此链接中找到有关令牌获取的一些相关详细信息:
https://stackoverflow.com/questions/44783736/how-to-list-images-and-tags-from-the-gcr-io-docker-registry-using-the-http-api

注意:如果您可以访问多个项目,则需要从响应中筛选出特定于项目的镜像。

英文:

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:
https://stackoverflow.com/questions/44783736/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

huangapple
  • 本文由 发表于 2022年1月10日 20:56:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/70652763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定