Saturday, January 1, 2022

How to push Docker images to AWS Elastic Container Registry (ECR)

 As an initial step to loading of docker images in Amazon ECR, Host machine installed with Docker and AWS CLI or AWSCLIV2

Docker on Ubuntu: https://devops-learner.blogspot.com/2019/10/how-to-install-and-setup-docker-in.html

Docker on AWS EC2: https://devops-learner.blogspot.com/2021/11/how-to-install-docker-on-amazon-ec2.html


Step 1: Let`s create dockerfile with a customized Apache httpd server 



Step 2: Type the following command to build the Docker image using dockerfile

 $ docker build -t apache-server -f httpd-Dockerfile .



Step 3: It will create a docker image, and you can check it by typing:

 $ docker images



Step 4: Just for testing purposes, let's run a docker container using this docker image to check if everything works fine at the local host! We will run this container at port 9091 of localhost. Type the following command for that :

 $ docker run -it --name apache-server -p 9091:80 httpd:latest








Now, since our docker image named “httpd” is been already created, it's time to move that image to AMAZON ECR!

 Step 5: Log in to your amazon aws console and search for an ECR service to get started:








Step 6: Now, our repository named “apache-server” is been created to save all our docker images! ON the upper right corner, you can see the “View push commands” named tab. You need to click on that and you will see something like this:


Just to cross-check the created repository have any images 


Step 7: Now go to your local OS( in my case it's Ubuntu 20.04 LTS ) where your docker image is saved and follow the above instructions! But before that you need to type the following two commands to configure your AWS account first :

AWS CLI Installation steps from the below links

AWS CLIV2 on Windows: https://devops-learner.blogspot.com/2021/11/how-to-install-aws-cli-in-windows-10-64.html

AWS CLIV2 on Linux: https://devops-learner.blogspot.com/2022/01/how-to-install-aws-cli2-on-linux-ubuntu.html

Note: Login as the root user

 Step 8: Once you type aws configure, it will ask the whole set of information to configure your account, like "access key", "secret access key", "region name" etc.Provide all the details and make sure your AWS user has permission to access AMAZON ECR service.

$ aws configure 


Note: In the case of Ubuntu 20 LTS ".docker" directory is not created by default, you need to create the directory manually using "mkdir .docker" and create a file called "vi config.json" inside the .docker directory.
$ mkdir .docker

$ vi config.json

Step 9: Once the ".docker" and "config.json" were created then update the necessary AWS ECR login credentials in the config.json file.











 Step 10:  once you get "Login succeeded", you are good to send your images to AWS ECR. Now you need to tag the image before you push it to the repo. Since our image is already created by <name>:<tag> i.e. apache-server:latest, let us tag this image, but here is the catch


docker tag <local_image>:<local_image_tag> <AWS_ECR_repository_url>:<image_Tag_you_wanna_proivde_in_ECR>

Once it is tagged successfully, we can check by using the "docker images" command to verify the tagged image



Step 11: Time to push the newly tagged image to the ECR repository and later Login to aws console and check ECR service if our image is pushed successfully!

$ docker push <AWS_12_DIGIT_USER_ID>.dkr.ecr.us-west-2.amazonaws.com/apache-server:latest

Before Image pushing to Repository :

After Image Pushed to AWS ECR Repository:


Hurray !!! We have pushed our custom image successfully to AWS ECR Repository ...



No comments:

Post a Comment

How to install and setup Kubernetes cluster using kOps in AWS environment

  Kops: Kops is also known as Kubernetes Operations, it is an open-source project which helps you to create, upgrade, destroy, and maintain ...