如何在Go语言代码中从Harbor拉取Docker镜像

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

How to pull docker image from Harbor in go lang code

问题

我想从Harbor注册表下载Docker镜像。就像下面的URL中从AWS ECR注册表下载镜像一样。

https://aemdemir.com/posts/how-to-pull-docker-images-from-amazon-ecr-private-registry-in-go/

我有点困惑如何为Harbor实现Authconfig。

英文:

I want to download the docker image from the Harbor registry.
Just like in the below url the image is being downloaded from the AWS ECR registry.

https://aemdemir.com/posts/how-to-pull-docker-images-from-amazon-ecr-private-registry-in-go/

I am bit confused how will i implement the Authconfig for the harbor.

答案1

得分: 1

最后我解决了这个问题。这是我第一次回答自己的问题。

所以,为了从Golang中的Harbor注册表中拉取私有Docker镜像,我们将使用以下库。

  1. import (
  2. "context"
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "os"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/registry"
  10. "github.com/docker/docker/client"
  11. "github.com/rs/zerolog/log"
  12. )
  13. func ImagePullHarbor(img string) error {
  14. ctx := context.Background()
  15. authConfig := registry.AuthConfig{
  16. Username: "harbor_registry_username",
  17. Password: "harbor_registry_password",
  18. ServerAddress: "harbor_registry_url",
  19. }
  20. encodedJSON, err := json.Marshal(authConfig)
  21. if err != nil {
  22. panic(err)
  23. }
  24. authStr := base64.URLEncoding.EncodeToString(encodedJSON)
  25. // 使用docker客户端拉取镜像
  26. dockerCli, err := newDockerClient()
  27. if err != nil {
  28. log.Error().Msgf("failed to create docker client: %w", err)
  29. return err
  30. }
  31. out, err := dockerCli.ImagePull(ctx, img, types.ImagePullOptions{RegistryAuth: authStr})
  32. if err != nil {
  33. return fmt.Errorf("failed to pull image: %w", err)
  34. }
  35. defer out.Close()
  36. _, err = io.Copy(os.Stdout, out)
  37. if err != nil {
  38. return fmt.Errorf("failed to read image logs: %w", err)
  39. }
  40. return err
  41. }
  42. func newDockerClient() (*client.Client, error) {
  43. cli, err := client.NewClientWithOpts(client.FromEnv)
  44. if err != nil {
  45. return nil, fmt.Errorf("failed to create docker client 1: %w", err)
  46. }
  47. return cli, nil
  48. }

在Golang中运行上述函数后,运行以下命令:

  1. docker images

您将看到已拉取的镜像。

额外提示:您可能需要运行 sudo chmod 666 /var/run/docker.sock

英文:

Finally I solved the problem. This is the first time I am answering to my question.

So to pull a private docker image from the harbor registry from golang we will be using the following libraries.

  1. import (
  2. "context"
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "os"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/registry"
  10. "github.com/docker/docker/client"
  11. "github.com/rs/zerolog/log")
  12. func ImagePullHarbor(img string) error {
  13. ctx := context.Background()
  14. authConfig := registry.AuthConfig{
  15. Username: "harbor_registry_username",
  16. Password: "harbor_registry_password",
  17. ServerAddress: "harbor_registry_url",
  18. }
  19. encodedJSON, err := json.Marshal(authConfig)
  20. if err != nil {
  21. panic(err)
  22. }
  23. authStr := base64.URLEncoding.EncodeToString(encodedJSON)
  24. # docker client is use to pull the image.
  25. dockerCli, err := newDockerClient()
  26. if err != nil {
  27. log.Error().Msgf("failed to create docker client: %w", err)
  28. return err
  29. }
  30. out, err := dockerCli.ImagePull(ctx, img, types.ImagePullOptions{RegistryAuth: authStr})
  31. if err != nil {
  32. return fmt.Errorf("failed to pull image: %w", err)
  33. }
  34. defer out.Close()
  35. _, err = io.Copy(os.Stdout, out)
  36. if err != nil {
  37. return fmt.Errorf("failed to read image logs: %w", err)
  38. }
  39. return err
  40. }
  41. func newDockerClient() (*client.Client, error) {
  42. cli, err := client.NewClientWithOpts(client.FromEnv)
  43. if err != nil {
  44. return nil, fmt.Errorf("failed to create docker client 1: %w", err)
  45. }
  46. return cli, nil
  47. }

After running the above function from golang. Run

> docker images

You can see the pulled images.

Bonus: You may have to run sudo chmod 666 /var/run/docker.sock.

huangapple
  • 本文由 发表于 2023年6月5日 12:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403447.html
匿名

发表评论

匿名网友

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

确定