在google云容器注册表的高级身份验证方法文档中,解释了使用带有docker cli的JSON密钥文件登录到注册表的方法,这很好
$ docker登录-u _json_key -p "$(cat keyfile.json)“https://gcr.io
但是,我试图使用同一个keyfile.json文件登录到注册表,使用golang docker/engine-api
库,我有一些工作代码,这似乎很好地验证到其他注册表,但总是提供以下结构的文件
{
"auths": {
"cr.whatever.com": {
"password": "PASSWORD",
"username": "registry"
}
}
}
将该Unmarshal
文件传递到ImageBuildOptions
函数这里,然后使用这里
但是,在使用keyfile.json
或工作config.json
时不起作用.
docker文档指出,应该使用带有用户名和密码的JSON base64编码对象,在标头参数部分中使用描述的这里。
我尝试了多个选项,以产生一个文件,可以成功地消费到码头X-Registry-Config
头,没有多少运气.
任何帮助/暗示都将不胜感激。
谢谢!
发布于 2017-12-07 16:19:32
感谢您的帮助金沙,我最终起草了一个工作代码函数如下
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
}
发布于 2017-12-06 22:56:43
我认为问题在于,“config”是一个过载的术语。看起来X-Registry-Config
包含一个options.AuthConfigs编码值的base64 64编码版本,而不是完整的docker配置文件。如果您只想对https://gcr.io
进行身份验证,您的JSON输入应该是:
{
"gcr.io": {
"username": "_json_key",
"password": "{contents of keyfile.json}"
}
}
或者,如果你愿意使用Docker的金唇语:
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
是留给读者的练习:)
https://stackoverflow.com/questions/47682134
复制相似问题