我最近遇到了这种情况,我需要将 docker 镜像推送到 ECR 仓库。
要使此方法起作用,它需要 ImageManifest。
https://docs.aws.amazon.com/sdk-for-go/api/service/ecr/#ECR.PutImage
您可以从此命令生成清单
docker manifest inspect busybox
使用上述命令的 json 输出作为字符串
示例代码:
result, err := cli.PutImage(context.TODO(), &ecr.PutImageInput{
RepositoryName: aws.String("ecr-repo-name"),
ImageTag: aws.String("docker-image-tag"),
ImageManifest: aws.String("{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.docker.distribution.manifest.list.v2+json\",\n \"manifests\": [\n {\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"size\": 527,\n \"digest\": \"sha256:dca71257cd2e72840a21f0323234bb2e33fea6d949fa0f21c5102146f583486b\",\n \"platform\": {\n \"architecture\": \"amd64\",\n \"os\": \"linux\"\n }\n },\n {\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"size\": 527,\n \"digest\": \"sha256:9cd47e9327430990c932b19596f8760e7d1a0be0311bb31bab3170bec5f27358\",\n \"platform\": {\n \"architecture\": \"arm\",\n \"os\": \"linux\",\n \"variant\": \"v5\"\n }\n },\n {\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"size\": 527,\n \"digest\": \"sha256:842295d11871c16bbce4d30cabc9b0f1e0cc40e49975f538179529d7798f77d8\",\n \"platform\": {\n \"architecture\": \"arm\",\n \"os\": \"linux\",\n \"variant\": \"v6\"\n }\n },\n {\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"size\": 527,\n \"digest\": \"sha256:0dd359f0ea0f644cbc1aa467681654c6b4332015ae37af2916b0dfb73b83fd52\",\n \"platform\": {\n \"architecture\": \"arm\",\n \"os\": \"linux\",\n \"variant\": \"v7\"\n }\n },\n {\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"size\": 527,\n \"digest\": \"sha256:121373e88baca4c1ef533014de2759e002961de035607dd35d00886b052e37cf\",\n \"platform\": {\n \"architecture\": \"arm64\",\n \"os\": \"linux\",\n \"variant\": \"v8\"\n }\n },\n {\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"size\": 527,\n \"digest\": \"sha256:ccff0c7e8498c0bd8d4705e663084c25810fd064a184671a050e1a43b86fb091\",\n \"platform\": {\n \"architecture\": \"386\",\n \"os\": \"linux\"\n }\n },\n {\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"size\": 527,\n \"digest\": \"sha256:0dc4e9a14237cae2d8e96e9e310116091c5ed4934448d7cfd22b122778964f11\",\n \"platform\": {\n \"architecture\": \"mips64le\",\n \"os\": \"linux\"\n }\n },\n {\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"size\": 528,\n \"digest\": \"sha256:04ebe37e000dcd9b1386af0e2d9aad726cbd1581f82067bea5cd2532b1f06310\",\n \"platform\": {\n \"architecture\": \"ppc64le\",\n \"os\": \"linux\"\n }\n },\n {\n \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n \"size\": 528,\n \"digest\": \"sha256:c10e75f6e5442f446b7c053ff2f360a4052f759c59be9a4c7d144f60207c6eda\",\n \"platform\": {\n \"architecture\": \"s390x\",\n \"os\": \"linux\"\n }\n }\n ]\n}\n"),
})
另一种简单的方法是从以下命令获取令牌:
aws ecr get-login-password
使用上述命令中的令牌并使用 docker api 推送 docker 映像。此方法不需要添加图像清单。
func PushImage(ctx context.Context, cli *client.Client, target string) {
auth := types.AuthConfig{
Username: "AWS",
Password: "<output of aws ecr get-login-password>",
ServerAddress: "<aws_account_id.dkr.ecr.region.amazonaws.com>".
,
}
authBytes, _ := json.Marshal(auth)
authBase64 := base64.URLEncoding.EncodeToString(authBytes)
push, err := cli.ImagePush(ctx, target, types.ImagePushOptions{All: true,
RegistryAuth:authBase64})
if err != nil {
panic(err)
}
termFd, isTerm := term.GetFdInfo(os.Stderr)
err = jsonmessage.DisplayJSONMessagesStream(push, os.Stderr, termFd, isTerm, nil)
if err != nil {
panic(err)
}
}