无法使用docker engine-api和长期存在的JSON文件从GCRregistry中提取

在谷歌云容器registry的高级身份validation方法文档中解释了使用Docker Cli使用JSON密钥文件loginregistry的方法,这只是工作正常

$ docker login -u _json_key -p“$(cat keyfile.json)” https://gcr.io

不过,我试图使用相同的keyfile.json文件login到使用golang docker docker/engine-api库的registry,我有一些工作代码,这似乎是authentication罚款到其他registry,但始终提供一个文件以下结构

 { "auths": { "cr.whatever.com": { "password": "PASSWORD", "username": "registry" } } } 

通过将Unmarshal文件传入ImageBuildOptions函数,然后在这里使用

但是,当使用keyfile.json或工作的config.json不工作…

docker文档指出,应该使用带有用户名和密码的JSON base64编码对象, 这里描述的标题参数部分。

我已经尝试了多个选项来生成一个文件,可以成功消耗到docker的X-Registry-Config头没有太多运气…

任何帮助/提示将不胜感激。

谢谢!

我认为问题是'config'是一个超载的术语。 看起来像X-Registry-Config包含options.AuthConfigs的JSON编码值的base64编码版本,而不是完整的dockerconfiguration文件。 如果你只想validationhttps://gcr.io ,你的JSONinput应该是:

 { "gcr.io": { "username": "_json_key", "password": "{contents of keyfile.json}" } } 

或者,如果你愿意使用Docker的golang库:

 import ( "encoding/base64" "encoding/json" "net/http" "github.com/docker/engine-api/types" ) def addDockerAuthsHeader(keyfile_contents string, headers http.Header) error { authConfigs := map[string]types.AuthConfig{ "gcr.io": { Username: "_json_key", Password: keyfile_contents } } buf, err := json.Marshal(authConfigs) if err != nil { return err } headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf)) return nil } 

生成keyfile_contents留给读者一个练习:)

感谢您的帮助jsand ,我终于起草了一个工作代码函数如下

 func (d *DockerEngineClient) BuildImage(archive, modelId string, authConfigs map[string]types.AuthConfig) (types.ImageBuildResponse, error) { buildContext, err := os.Open(archive) defer buildContext.Close() c, err := ioutil.ReadFile(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_FILE")) if err != nil { return err } var authConfigs2 map[string]types.AuthConfig authConfigs2 = make(map[string]types.AuthConfig) authConfigs2["gcr.io"] = types.AuthConfig{ Username: "_json_key", Password: string(c), ServerAddress: fmt.Sprintf("https://%s", d.remoteRegistryPrefix), } buildOptions := types.ImageBuildOptions{ Tags: []string{fmt.Sprintf("%s/%s", d.remoteRegistryPrefix, modelId)}, AuthConfigs: authConfigs2, } var buildResponse types.ImageBuildResponse buildResponse, err = d.client.ImageBuild(context.TODO(), buildContext, buildOptions) if err != nil { return buildResponse, err } b, _ := ioutil.ReadAll(buildResponse.Body) fmt.Printf("%s\n", b) buildResponse.Body.Close() return buildResponse, err }