How to Build and Push Docker Images to AWS ECR

Yash Thaker
Devops Türkiye☁️ 🐧 🐳 ☸️
5 min readOct 10, 2022

Pushing an image from Docker hub to AWS Elastic Container Registry is pretty easy. The best case usage of this article is when you want to go all cloud and move all your images to AWS ECR, when Docker is becoming obsolete these days.

The requirements for this article include:

  1. Docker Hub Account
  2. AWS Account
  3. Basic Knowledge of Docker Cli Commands
  4. Basic Knowledge of AWS Cli Commands
  5. Any image or simple webapp image to get started to push it to ECR
  1. Installing Docker

I am using AWS Linux 2 AMI for this demo, but you can use any image you want. Here is the download link for other distributions incase the commands differ:

You can simple type yum install docker -y as AWS Linux 2 AMI has repositories already setup for ease of usage.

Start the service using:

systemctl start docker
systemctl enable docker
systemctl status docker

2. Docker Login

Now login into the instance with your Docker Hub credentials using

docker login

3. Creating an sample image to push to Docker Hub

Create a file named Dockerfile and add the content below which is an basic webapplication using apache-httpd.

You can skip this step if you already have an image.

FROM httpd:latestLABEL Author=username@email.comLABEL Version=0.1COPY index.html /usr/local/apache2/htdocs/

Create an file named index.html in the same directory and add the content “Hello World!” in it.

Let’s understand the Dockerfile:

  1. FROM: We are using httpd as a Base image. httpd is official image maintained by apache http project.
  2. LABEL: Labels are used in Dockerfile to help organize your Docker Images. Labels are key-value pairs and simply adds custom metadata to your Docker Images
  3. COPY: Copy is used to copy the files from the local machine to the image. We are copying it from the current working directory.

We have completed creating Dockerfile, let’s dive into creating a Docker image out of it.

4. Create docker image of the Dockerfile:

Use the below command and replace the <name-tag> with the image name you want:

docker build -t <name-tag> .

And check the image is created using docker images command.

Running the application using port forwarding:

We are port-forwarding the application which runs on 80 port to 80 port for accessing publically.

docker run -p 80:80 <name-tag>

You can access the application at port 80 as shown below:

You can also push this image to Docker hub by renaming the image with username of docker hub account and pushing it:

docker tag <name-tag> <dockerhub-username>/<image-name>
docker push <dockerhub-username>/<image-name>

4. Installing AWS Cli and Programmatic Access user in AWS:

You need to create a user with programmatic access to use AWS Cli and give

And provide appropriate policies, in my case I used AdministratorAccess for learning purpose. But would not recommend in production instances.

Configuring AWS Cli:

AWS Linux 2 AMI already has aws-cli installed but you can download from here if you are on other Linux Image using this link.

aws configure

Enter the access key and secret access key you got when you created a user.

5. Pushing Image to ECR

Create ECR Repo:

aws ecr create-repository --repository-name <repo_name> --region <region_name>

You can verify that repo is created by viewing console for ECR as shown below:

Pushing Docker image to ECR:

For pushing docker image to ECR we need to authenticate our Docker creds with AWS.

We use the get-login-password command that retrieves and displays an authentication token using the GetAuthorizationToken API that we can use to authenticate to an Amazon ECR registry.

aws ecr get-login-password --region <region_name>

And save the token somewhere for future use.

aws ecr --region <region> | docker login -u AWS -p <encrypted_token> <repo_uri>

Replace the token with above output and paste the repo uri from the console.

Tag Docker Image for ECR Repo:

docker tag <source_image_tag> <target_ecr_repo_uri>

source_image_tag : Is the image name we gave earlier while using docker build command.

target_ecr_repo_uri : URI of ECR Repo we created

Push Image to ECR:

docker push <ecr-repo-uri>

You can verify in the ECR Repo, that image has been uploaded

To Wrap up

In this article, we covered how to deploy a Docker image on AWS ECS. You can use this Docker container resting inside ECR to host your application on the server. It could be AWS EC2 or anything else.

Docker is being obsolete and people are moving to cloud. ECR is a great solution for achieving this, where majority of workloads are on AWS Cloud.

You can always automate this process with a script where you can push hundreds of images via single click.

If you enjoyed this story, please click the 👏 button and share to help others find it! Feel free to leave a comment below.

--

--

Yash Thaker
Devops Türkiye☁️ 🐧 🐳 ☸️

DevOps Engineer & Blogger | Automating Infrastructure for High Performance Apps | Sharing Practical Insights & Best Practices | Exploring New Tech & Techniques