Wednesday, March 10, 2021

Push images to AWS ECR




In this post we will review how to push docker images to AWS Container Registry (aka ECR). The instructions here are intended to be used as part of a CLI to automate the push.


Start by authenticating to the AWS, and setting the configuration. You can use the env.sh file in this post.


Next authenticate to the docker:


aws ecr get-login-password --region REGION | docker login --username AWS --password-stdin USERID.dkr.ecr.REGION.amazonaws.com


Replace the USERID and the REGION with the relevant values for your deployment, for example:


aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 234567786.dkr.ecr.us-east-1.amazonaws.com


AWS ECR does not support sub folders within the registry, and so we need to configure an ECR per image.  This can be done using the following script:


repoName=my-image/dev
aws ecr describe-repositories --repository-names ${repoName} >/dev/null 2>&1
status=$?
if [[ ! "${status}" -eq 0 ]]; then
aws ecr create-repository --repository-name ${repoName}
fi


The last step is to tag the image and to push the image to the ECR.


imageName=my-image/dev:latest
EcrTag=USERID.dkr.ecr.REGION.amazonaws.com/${imageName}
LocalTag=MY-LOCAL-REGISTRY/${imageName}
docker tag ${LocalTag} ${EcrTag}
docker push ${EcrTag}
docker rmi ${EcrTag}



Final Note


In this post we have reviewed the steps to automate push of docker image to AWS ECR. The ECR can be latest used by a EKS as a registry container. To accomplish this, make sure to add permission to the EKS account to access the ECR.


No comments:

Post a Comment