Getting Started with Amazon ECR

Aamod Kadam
3 min readNov 16, 2024

--

How to push docker image to Amazon ECR ?

Amazon Elastic Container Registry (ECR) is the managed container registry. ECR can be used to store Docker images, Open Container Initiative (OCI) images and OCI compatible artefacts such as Helm Charts.

This post focuses on how to create a repository and push docker image to ECR using AWS CLI. There could be various scenarios and you may want to push the image from various locations. This post focuses on using AWS CloudShell to push the image.

Step — Start the CloudShell

AWS CloudShell

Step — Create repository

aws ecr create-repository — repository-name demo/sample-repo
Repository Created

Few points to note are:

  • This is a private repository so only authorised users can access it
  • Tag immutability is a feature that prevents image tags from being overwritten. It is a best practice to set it to Immutable for PROD repositories. However in this case it is Mutable as this is a demo repository.

Step — Build Demo Docker Image

# Create Docker file
touch Dockerfile

Put the following content in Dockerfile. This simply installs httpd and creates an html page with Hello World !

FROM public.ecr.aws/amazonlinux/amazonlinux:latest

# Install dependencies
RUN yum update -y && \

yum install -y httpd

# Install apache and write hello world message
RUN echo 'Hello World!' > /var/www/html/index.html

# Configure apache
RUN echo 'mkdir -p /var/run/httpd' >> /root/run_apache.sh && \

echo 'mkdir -p /var/lock/httpd' >> /root/run_apache.sh && \

echo '/usr/sbin/httpd -D FOREGROUND' >> /root/run_apache.sh && \

chmod 755 /root/run_apache.sh

EXPOSE 80

CMD /root/run_apache.sh

Step — Build image

docker build -t hello-world .
docker build output
hell-world image listed

Step — Push the image to ECR repo

ECR requires different credentials (docker login credentials) to connect and those are retrieved in the following step.


# get the AWS_ACCOUNT_ID
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
# construct the ECR URL
ECR_URL=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com

#get login-password for ECR and do docker login to ECR
aws ecr get-login-password | docker login --username AWS --password-stdin ${ECR_URL}
docker Login succeeded

Step- Tag the existing image with ECR repo tag

# tag the existing image with different tag
docker tag hello-world 2x7x0x4xxxxx.dkr.ecr.ap-south-1.amazonaws.com/demo/sample-repo
tagged image

Note the size of the image 280 MB.

Step — Push the tagged image

docker push 2x7x0x4xxxxx.dkr.ecr.ap-south-1.amazonaws.com/demo/sample-repo
Pushed image to ECR

Step — Verify from AWS Console

The docker image is pushed to ECR repo ! If you notice the Size of image in ECR is 130 MB as against of 280 MB seen earlier . ECR saves the image in a compressed way and in this case it has provided more than 50% of saving in storage space !

That is all for this post !

--

--

Aamod Kadam
Aamod Kadam

Written by Aamod Kadam

AWS Community Builder | upGrad Course Author | 7 x AWS | Terraform Associate | Cloud Consulting | AWS | Azure | Docker | Kubernetes | Software Architecture

No responses yet