簡體   English   中英

如何使用 golang 從自定義 docker 注冊表中提取圖像?

[英]How to pull image from custom docker registry with golang?

使用 docker 源如何從自定義注冊表中提取圖像? 由於使用了這樣的代碼

// Prepare auth registry for usage
func (app *App) PrepareRegistry() error {
    app.AuthConfig = types.AuthConfig{
        Username:      Username,
        Password:      Password,
        ServerAddress: DefaultServer,
    }

    resp, err := app.Client.RegistryLogin(context.Background(), app.AuthConfig)
    if err != nil {
        panic(err)
    }

    fmt.Println(resp.Status)
    if resp.IdentityToken != "" {
        app.AuthConfig.IdentityToken = resp.IdentityToken
    }

    app.AuthConfigEncoded, err = command.EncodeAuthToBase64(app.AuthConfig)
    return err
}

func (app *App) ImagePull() error {

    opts := types.ImagePullOptions{
        All:            true,
        RegistryAuth: app.AuthConfigEncoded,
        PrivilegeFunc: registryAuthentication(app.Name),
    }
    responseBody, err := app.Client.ImagePull(context.Background(), app.Name, opts)
    defer responseBody.Close()
    if err != nil {
        return err
    }
    return nil
}

我仍然收到錯誤

Login Succeeded
panic: Error response from daemon: Get https://registry-1.docker.io/v2/shalakhin/blender/tags/list: unauthorized: incorrect username or password

ServerAddress 是 registry.gitlab.com,而不是 registry-1.docker.io

你檢查身份令牌了嗎? 這可能會導致身份驗證問題。

一條建議:
Docker客戶端

這很好,因為我可以看到你沒有指定端點 我想你應該添加這個信息。

authConfig := types.AuthConfig{
    Username: "username",
    Password: "password",
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
    panic(err)
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)

out, err := cli.ImagePull(ctx, "alpine", types.ImagePullOptions{RegistryAuth: authStr})
if err != nil {
    panic(err)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM