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

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

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镜像,我们将使用以下库。

import (
	"context"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"os"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/registry"
	"github.com/docker/docker/client"
	"github.com/rs/zerolog/log"
)

func ImagePullHarbor(img string) error {
	ctx := context.Background()

	authConfig := registry.AuthConfig{
		Username:      "harbor_registry_username",
		Password:      "harbor_registry_password",
		ServerAddress: "harbor_registry_url",
	}

	encodedJSON, err := json.Marshal(authConfig)
	if err != nil {
		panic(err)
	}

	authStr := base64.URLEncoding.EncodeToString(encodedJSON)

	// 使用docker客户端拉取镜像
	dockerCli, err := newDockerClient()
	if err != nil {
		log.Error().Msgf("failed to create docker client: %w", err)
		return err
	}

	out, err := dockerCli.ImagePull(ctx, img, types.ImagePullOptions{RegistryAuth: authStr})
	if err != nil {
		return fmt.Errorf("failed to pull image: %w", err)
	}
	defer out.Close()

	_, err = io.Copy(os.Stdout, out)
	if err != nil {
		return fmt.Errorf("failed to read image logs: %w", err)
	}
	return err
}

func newDockerClient() (*client.Client, error) {
	cli, err := client.NewClientWithOpts(client.FromEnv)
	if err != nil {
		return nil, fmt.Errorf("failed to create docker client 1: %w", err)
	}
	return cli, nil
}

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

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.

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
"github.com/rs/zerolog/log")
func ImagePullHarbor(img string) error {
ctx := context.Background()
authConfig := registry.AuthConfig{
Username:      "harbor_registry_username",
Password:      "harbor_registry_password",
ServerAddress: "harbor_registry_url",
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
panic(err)
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
# docker client is use to pull the image.
dockerCli, err := newDockerClient()
if err != nil {
log.Error().Msgf("failed to create docker client: %w", err)
return err
}
out, err := dockerCli.ImagePull(ctx, img, types.ImagePullOptions{RegistryAuth: authStr})
if err != nil {
return fmt.Errorf("failed to pull image: %w", err)
}
defer out.Close()
_, err = io.Copy(os.Stdout, out)
if err != nil {
return fmt.Errorf("failed to read image logs: %w", err)
}
return err
}
func newDockerClient() (*client.Client, error) {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, fmt.Errorf("failed to create docker client 1: %w", err)
}
return cli, nil
}

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:

确定