如何为私有注册表登录凭据创建“RegistryAuth”?

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

How to create “RegistryAuth” for Private Registry Login Credentials

问题

我使用以下命令创建了一个私有注册表:

docker run -d -p 5000:5000 --restart=always --name registry -e REGISTRY_STORAGE_DELETE_ENABLED=true registry:2

我正在尝试使用 Golang 的 Docker 客户端 API(ImagePush)将镜像推送到该注册表。

func (cli *Client) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error)

当我查看 ImagePushOptions 时,我发现结构体如下:

type ImagePullOptions struct {
    # All bool
    RegistryAuth string // RegistryAuth 是注册表的 base64 编码凭据
    PrivilegeFunc RequestPrivilegeFunc
}

有没有办法创建 RegistryAuth 字符串?

我尝试了以下方法:

type AuthArgs struct {
    Username string json:"username"
    Password string json:"password"
    Email string json:"email"
    ServerAddress string json:"serveraddress"
}

func() {
    m := AuthArgs{"docker", "docker", "", "localhost:5000"}
    b, err := json.Marshal(m)

    fmt.Println(string(b))
    encodeStr := base64.StdEncoding.EncodeToString(b)

    refStr := "localhost:5000/" + image + ":" + tag
    fmt.Println(refStr)
    resp, err := cli.ImagePush(ctx, refStr, types.ImagePushOptions{RegistryAuth: encodeStr })

我已经卡在这里三天了,任何帮助都将是极好的。

英文:

I created a Private Registry using following command:

docker run -d -p 5000:5000 --restart=always --name registry -e REGISTRY_STORAGE_DELETE_ENABLED=true registry:2

I am trying to push Images to this registry using Golang docker client API -> ImagePush

func (cli *Client) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error)

When I zoom into ImagePushOptions I see that struct is

type ImagePullOptions struct {
    # All bool
    RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry
    PrivilegeFunc RequestPrivilegeFunc
}

AnyIdea how to create RegistryAuth String

I tried doing following:

type AuthArgs struct {
    Username string json:"username"
    Password string json:"password"
    Email string json:"email"
    ServerAddress string json:"serveraddress"
}

func() {
    m := AuthArgs{"docker", "docker", "", "localhost:5000"}
    b, err := json.Marshal(m)

    fmt.Println(string(b))
    encodeStr := base64.StdEncoding.EncodeToString(b)

    refStr := "localhost:5000/" + image + ":" + tag
    fmt.Println(refStr)
    resp, err := cli.ImagePush(ctx, refStr, types.ImagePushOptions{RegistryAuth: encodeStr })

I am stuck from 3 days, any help would be great

答案1

得分: 7

以下是要翻译的内容:

auth := types.AuthConfig{
    Username: cfg.User,
    Password: cfg.Passwd,
}
authBytes, _ := json.Marshal(auth)
authBase64 := base64.URLEncoding.EncodeToString(authBytes)

https://github.com/leopoldxx/godocker/blob/master/docker.go

请注意,这是一段Go语言代码,用于创建Docker身份验证配置并将其编码为Base64字符串。你提供的链接指向一个名为docker.go的文件,可能包含与此相关的更多代码。

英文:
auth := types.AuthConfig{
    Username: cfg.User,
    Password: cfg.Passwd,
}
authBytes, _ := json.Marshal(auth)
authBase64 := base64.URLEncoding.EncodeToString(authBytes)

https://github.com/leopoldxx/godocker/blob/master/docker.go

huangapple
  • 本文由 发表于 2017年3月4日 07:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/42590211.html
匿名

发表评论

匿名网友

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

确定