英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论