如何从golang自定义docker注册表中提取图像?

icnyk63a  于 2023-01-20  发布在  Docker
关注(0)|答案(2)|浏览(134)

使用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

当服务器地址为registry.gitlab.com而非registry-1.docker.io

bt1cpqcv

bt1cpqcv1#

是否检查了标识令牌?这可能会导致身份验证问题。
建议:
Docker client
这工作正常,因为我可以看到你没有指定端点。我认为你应该添加这个信息。

mrwjdhj3

mrwjdhj32#

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)
}

相关问题