'Docker Image tagging in ECR

I am pushing docker image in AWS ECR using Jenkins.

While pushing the image I am providing tag as $Build_Number. So in ECR repo I have images with tags like 1,2,3,4.

But when I am trying to pull the image from EC2 with below command from Jenkins job

docker pull 944XXX.dkr.ecr.us-east-1.amazonaws.com/repository1:latest

I am getting an error as there is no image with tag as latest.

Here I want to pull the latest image (with tag 4). I cannot hard-code tag number here as docker pull command will run from Jenkins job automatically. So what way can I pull the latest image?



Solution 1:[1]

one solution is suggested by @Lix that you can try, or if you are interested with just latest pushed image and no matter whats the tag of a latest image then you can get the latest image from AWS-CLI.

So your Jenkins job command will be

TAG=$(aws ecr describe-images --output json --repository-name stage/redis --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]' | jq . --raw-output)
docker pull 944XXX.dkr.ecr.us-east-1.amazonaws.com/repository1:$TAG

aws-cli-ecr-list-images-get-newest

Solution 2:[2]

If you want a latest tag on ECR, you need to add it and push it there when you build the image. You can use the -t to docker build multiple times (see https://docs.docker.com/v17.09/engine/reference/commandline/build/); just make sure to push them all.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adiii
Solution 2 Daniel Farrell