使用没有 docker 守护进程依赖的 go docker 客户端查询 docker 注册表 (hub.docker.com)

query a docker registry (hub.docker.com) using go docker client without docker daemon dependency

我正在尝试使用 Go 访问 docker 注册表(public 或私有)。一个简单的程序,可以访问任何注册表并验证图像是否存在。

我查看了 docker Go 中可用的客户端 https://pkg.go.dev/github.com/docker/docker@v20.10.11+incompatible/client

但问题是,此客户端需要 docker 守护程序 运行ning 才能工作。有什么方法可以查询 docker 注册表(例如:hub.docker.com)而不依赖于底层 docker 引擎?

我的想法是 运行 这个程序在 docker 容器上,容器内不会有任何 docker 引擎 运行ning。而且我不想 运行 docker 里面 docker 或任何类型的 hack。我只想连接到注册表并查询图像。并且请不要在堆栈溢出中引用其他问题。没有人回答这个问题。

这是我目前所做的


import (
    "context"
    "encoding/base64"
    "encoding/json"
    "fmt"
    "github.com/docker/docker/api/types/filters"
    "time"

    "github.com/docker/docker/api/types"
    "github.com/docker/docker/client"
)

func main() {


    cli, err := client.NewClientWithOpts(client.WithHost("https://hub.docker.com"), client.WithAPIVersionNegotiation())
    if err != nil {
        fmt.Println(err.Error())
        return
    }


    err = imagemanifest(cli)
    if err != nil {
        fmt.Println(err)
    }

    err = imageSearch(cli)

}


func imagemanifest(dockerClient *client.Client) error {

    var authConfig = types.AuthConfig{
        Username:      "amokkara",
        Password:      "M@vr1ck2009",
        ServerAddress: "https://index.docker.io/v2/",
    }

    ctx, cancel := context.WithTimeout(context.Background(), time.Second*1200)
    defer cancel()

    authConfigBytes, _ := json.Marshal(authConfig)
    authConfigEncoded := base64.URLEncoding.EncodeToString(authConfigBytes)

    ctx, cancel = context.WithTimeout(context.Background(), time.Second*1200)
    defer cancel()


    searchres , err := dockerClient.DistributionInspect(ctx,"amokkara/amokkara:3",authConfigEncoded)
    if err != nil {
        return err
    }

    fmt.Println(searchres.Descriptor.Digest.String())

    return nil
}





如果我这样初始化客户端

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())

这是有效的,因为它使用底层 docker 守护进程(在我的例子中是 docker 桌面)来查询注册表。但是如果使用

创建客户端

client.NewClientWithOpts(client.WithHost("https://hub.docker.com"), client.WithAPIVersionNegotiation())

它没有给出 404 错误。此客户端是否需要 docker 守护程序才能工作。如果是这样,我还有其他方法可以查询注册表吗?请帮我解决这个问题。

Skopeo 是在没有守护进程的情况下处理注册表的领先软件。

也是用Go写的

您可以从 inspect.go

中获得启发

请注意,您不需要使用 github.com/docker/docker/* 个模块,但会是 github.com/containers/*,即 https://github.com/containers/image