如何使用golang sdk将docker镜像推入aws ecr

时间:2018-01-08 15:17:12

标签: amazon-web-services docker go aws-sdk aws-ecs

我正在尝试编写一个工具来自动将泊坞窗图像推入aws ECR。

我正在尝试使用aws golang sdk将docker镜像推入aws ECR。 我试图遵循此文档https://docs.aws.amazon.com/sdk-for-go/api/service/ecr/#ECR.PutImage

但不知道如何制作ImageManifest对象https://docs.aws.amazon.com/sdk-for-go/api/service/ecr/#PutImageInput 非常感谢帮助。

2 个答案:

答案 0 :(得分:0)

文件清楚地说:

  

此操作由Amazon ECR代理使用,并非有意   供客户一般用于拉动和推动图像。多数情况   在这种情况下,您应该使用docker CLI来提取,标记和推送图像。

相反,我会建议您使用exec Go包并直接在构建计算机中实现exec命令。

您可以这样做:

package main

import (
    "fmt"
    "os"
    "os/exec"
)

func main() {
    name := "docker"
    args := []string{"push", "registry/name:tag"}
    cmd := exec.Command(name, args...)
    if err := cmd.Run(); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    fmt.Println("done")
}

我从此博客https://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/

中提取了此示例

我希望你能有这个信息有用

答案 1 :(得分:0)

我最近遇到了这种情况,我需要将 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)
    }
}