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

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

How to create “RegistryAuth” for Private Registry Login Credentials

问题

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

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

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

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

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

  1. type ImagePullOptions struct {
  2. # All bool
  3. RegistryAuth string // RegistryAuth 是注册表的 base64 编码凭据
  4. PrivilegeFunc RequestPrivilegeFunc
  5. }

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

我尝试了以下方法:

  1. type AuthArgs struct {
  2. Username string json:"username"
  3. Password string json:"password"
  4. Email string json:"email"
  5. ServerAddress string json:"serveraddress"
  6. }
  7. func() {
  8. m := AuthArgs{"docker", "docker", "", "localhost:5000"}
  9. b, err := json.Marshal(m)
  10. fmt.Println(string(b))
  11. encodeStr := base64.StdEncoding.EncodeToString(b)
  12. refStr := "localhost:5000/" + image + ":" + tag
  13. fmt.Println(refStr)
  14. resp, err := cli.ImagePush(ctx, refStr, types.ImagePushOptions{RegistryAuth: encodeStr })

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

英文:

I created a Private Registry using following command:

  1. 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

  1. 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

  1. type ImagePullOptions struct {
  2. # All bool
  3. RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry
  4. PrivilegeFunc RequestPrivilegeFunc
  5. }

AnyIdea how to create RegistryAuth String

I tried doing following:

  1. type AuthArgs struct {
  2. Username string json:"username"
  3. Password string json:"password"
  4. Email string json:"email"
  5. ServerAddress string json:"serveraddress"
  6. }
  7. func() {
  8. m := AuthArgs{"docker", "docker", "", "localhost:5000"}
  9. b, err := json.Marshal(m)
  10. fmt.Println(string(b))
  11. encodeStr := base64.StdEncoding.EncodeToString(b)
  12. refStr := "localhost:5000/" + image + ":" + tag
  13. fmt.Println(refStr)
  14. resp, err := cli.ImagePush(ctx, refStr, types.ImagePushOptions{RegistryAuth: encodeStr })

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

答案1

得分: 7

以下是要翻译的内容:

  1. auth := types.AuthConfig{
  2. Username: cfg.User,
  3. Password: cfg.Passwd,
  4. }
  5. authBytes, _ := json.Marshal(auth)
  6. authBase64 := base64.URLEncoding.EncodeToString(authBytes)
  7. https://github.com/leopoldxx/godocker/blob/master/docker.go

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

英文:
  1. auth := types.AuthConfig{
  2. Username: cfg.User,
  3. Password: cfg.Passwd,
  4. }
  5. authBytes, _ := json.Marshal(auth)
  6. 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:

确定