Know Nothing
Know Nothing

Reputation: 1251

How to push docker image into aws ecr using golang sdk

I am trying to write a tool to automatically push docker image into aws ECR.

I am trying to push a docker image into aws ECR using aws golang sdk. was trying to follow this documentation https://docs.aws.amazon.com/sdk-for-go/api/service/ecr/#ECR.PutImage

but no clue how to make ImageManifest object https://docs.aws.amazon.com/sdk-for-go/api/service/ecr/#PutImageInput Help is very much appreciated.

Upvotes: 2

Views: 2106

Answers (1)

Amjad Hussain Syed
Amjad Hussain Syed

Reputation: 1040

I recently come across this situation where I need to push docker image to ECR repo.

For this method to work it requires the ImageManifest.

https://docs.aws.amazon.com/sdk-for-go/api/service/ecr/#ECR.PutImage

You can generate the manifests from this command

docker manifest inspect busybox

Use the json output from the above command as string

Sample Code:

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

Another easy way is to get the token from the below command:

aws ecr get-login-password

use the token from the above command and push docker image using the docker api. This method doesn't require to add Image manifests.

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

Upvotes: 3

Related Questions