{"id":1644,"date":"2022-06-22T11:09:33","date_gmt":"2022-06-22T11:09:33","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=1644"},"modified":"2023-08-06T15:33:47","modified_gmt":"2023-08-06T15:33:47","slug":"build-docker-image-to-aws-ecr","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/build-docker-image-to-aws-ecr","title":{"rendered":"How to Build a Docker Image and Push it To AWS ECR?"},"content":{"rendered":"<h3>Why do we need docker container?<\/h3>\n<p>Did you ever listened of a problem from your teammates that the code you have shared is not working but it was perfectly alright when you used it, ok ,let&#8217;s understand this with a practical scenario , assume you are a developer in your organization ,you have developed a new feature and it&#8217;s perfectly working for you and you forwarded it to testing team for testing , then you get a reply from them that the newly added feature is not at all working for them, what went wrong here ? , may be the testing team do not have the dependencies required or even though they installed all the required dependencies but a different version or you are working on latest version of OS and they are using older versions, to overcome these kind of problems we have got a solution called containerization. A <b>Container packages<\/b>\u00a0up code and all the required dependencies so that the application runs without any error from one computing environment to another, then what is Docker?<\/p>\n<p><strong>Docker<\/strong> is an open source and free containerization platform, It allows <a href=\"https:\/\/www.checkmateq.com\/full-stack-development\">full stack developers<\/a> to package applications into executable components by combining application source code with the operating system\u00a0 libraries and dependencies required to execute that code in any environment, developers can also create containers without using docker, but docker\u00a0 makes it easier\u00a0 to build, deploy cicd pipeline and manage containers using simple commands, you can create a docker container using a docker image, docker images act as a set of instructions or a template to build a Docker container, you can compare a docker image to a snapshot in virtual machine environments, where you can take a snapshot of VM and create similar ones. you can share these images with your team or publicly by uploading them to a container registry there are many container registries in this blog we will look into AWS Elastic Container Registry<\/p>\n<h3><strong>Elastic Container Registry<\/strong><\/h3>\n<p>ECR is fully managed docker container registry provided by <a href=\"https:\/\/www.checkmateq.com\/aws-cloud\">AWS<\/a> for hosting docker images , so you need not to worry about hosting your own private\u00a0 container registry , AWS will take care of underline infrastructure like scalability and high availability\u00a0 , and where ever you are going to deploy your docker container like Kubernetes or amazon EKS those container orchestration platform can pull the image from amazon ECS, follow below procedure to create a repository in AWS and upload an Image to it.<\/p>\n<p>In this blog we will create a docker image in an AWS ec2 instance and upload it to <strong>Amazon Elastic Container Registry,\u00a0<\/strong>if you are using your local machine skip step-3 from the whole process and if want to create an AWS ec2 instance refer this <a href=\"https:\/\/www.checkmateq.com\/blog\/how-to-launch-ec2-instance-in-aws\/\">blog<\/a><\/p>\n<h3><b>Step-1: Create ECR cluster<\/b><\/h3>\n<p>Login to your aws cloud console search for ECR and click on it.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1648 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-300x154.jpeg\" alt=\"\" width=\"666\" height=\"342\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-300x154.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1024x527.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-768x395.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1536x790.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1200x617.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2.jpeg 1895w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Click on &#8220;Get Started&#8221; under Create a repository<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1649 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-300x154.jpeg\" alt=\"\" width=\"677\" height=\"347\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-300x154.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1024x526.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-768x395.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1536x789.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1200x617.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3.jpeg 1895w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Select &#8216;Public&#8217; or &#8216;Private&#8217; under Visibility settings, if you select public your repository will be accessible by everyone to pull the stored images and for private you can give access using IAM roles, for the purpose of this blog I am creating a private repository, give a name to your container repository.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1759 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-1-1-300x153.jpeg\" alt=\"\" width=\"691\" height=\"352\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-1-1-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-1-1-1024x521.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-1-1-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-1-1-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-1-1-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-1-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Click on the name or ECR you have created<\/p>\n<p><img loading=\"lazy\" class=\"wp-image-1760 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1-300x152.jpeg\" alt=\"\" width=\"715\" height=\"362\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1-1024x520.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1-768x390.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1-1536x780.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1-1200x609.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-2-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>then click on view push commands, it will show you the commands to login in to ECR, create a docker image and how to tag it and push it to your container repository.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1761 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-4-1-300x152.jpeg\" alt=\"\" width=\"707\" height=\"358\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-4-1-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-4-1-1024x520.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-4-1-768x390.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-4-1-1536x780.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-4-1-1200x609.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-4-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1762 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1-300x161.jpeg\" alt=\"\" width=\"703\" height=\"377\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1-300x161.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1-1024x549.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1-768x412.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1-1536x823.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1-1200x643.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-3-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now let&#8217;s see how to Create a Docker image and push it to ECR<\/p>\n<h3><b>Step-2: Writing a Dockerfile\u00a0<\/b><\/h3>\n<p>before writing a Dockerfile let&#8217;s install docker first, use below command to install docker on your compute instance.<\/p>\n<pre>yum install docker -y<\/pre>\n<p>Check the installation using below given command.<\/p>\n<pre>docker --version<\/pre>\n<p>This command gives installed version of docker.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1795 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-19-300x32.jpeg\" alt=\"\" width=\"732\" height=\"78\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-19-300x32.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-19-1024x108.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-19-768x81.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-19-1536x161.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-19-1200x126.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-19.jpeg 1750w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Create a file named Docker in the root folder of the application, every time you create a new docker file create it in new directory and the file name should be starting with capital &#8220;D&#8221;, use below command to create Dockerfile where vi is an editor and copy the following syntax into the file and save it.<\/p>\n<p>vi Dockerfile<\/p>\n<p><strong>Dockerfile<\/strong><\/p>\n<pre>#Installing apache server on ubuntu\r\nFROM ubuntu\r\nRUN apt-get update\r\nRUN apt-get install apache2 -y\r\nRUN apt-get install apache2-utils -y\r\nRUN apt-get clean\r\nEXPOSE 80\r\nCMD [\"apache2ctl\", \"-D\",\"FOREGROUND\"]\r\n<\/pre>\n<p>each line in Dockerfile is executed as individual step and creates a layer in the Docker image, let&#8217;s understand it by breaking down above given docker file which install apache on ubuntu.<\/p>\n<p><strong>Line-1:<\/strong> #Installing apache server on ubuntu<\/p>\n<p>Start a line with &#8220;#&#8221; to write comments in a Dockerfile.<\/p>\n<p><strong>Line-2:<\/strong>\u00a0 FROM ubuntu<\/p>\n<p>Most of the time you will not create a docker image from scratch, you may use a docker image as a base to build your required image. The FROM keyword tells Docker which base image to use to build the new image, you can only have comments above FROM line but no other commands, In the above given Dockerfile , Docker uses ubuntu image as a base and will perform rest of the things on it.<\/p>\n<p><strong>Line-3: <\/strong>RUN apt-get update, RUN is used to give instructions to docker i.e., run a specific Linux command use RUN keyword.<\/p>\n<p><strong>Line-7:\u00a0 <\/strong>EXPOSE 80, this instruction exposes particular port with a specified protocol(tcp\/udp) inside a docker Container.<\/p>\n<p><strong>Line-8: <\/strong>CMD [&#8220;apache2ctl&#8221;, &#8220;-D&#8221;,&#8221;FOREGROUND&#8221;], instructions given with CMD will be executed only when a container is created, it has nothing to do with image building and you can always override these CMD instruction&#8217;s using external commands, in our case we are using it to run apache in background.<\/p>\n<h3><strong>Step-3: Allow EC2 instance to access ECR using IAM Role<\/strong><\/h3>\n<p><strong>\u00a0<\/strong>To push your Docker image to ECR you have to connect to it, one of the ways to connect is using IAM role if you are using an AWS ec2 instance, if you are trying to access from your local machine use access key and secret key, as we are using an ec2 instance now let&#8217;s go and create a IAM role which allows our ec2 instance to access ECR registry.<\/p>\n<p>On your AWS console search for IAM and click on it, then on the left pane click on <strong>Roles <\/strong>under &#8220;Access management&#8221;.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1764 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-5-1-300x153.jpeg\" alt=\"\" width=\"710\" height=\"362\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-5-1-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-5-1-1024x521.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-5-1-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-5-1-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-5-1-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-5-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Click on &#8216;create role&#8217;<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1765 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-6-1-300x152.jpeg\" alt=\"\" width=\"708\" height=\"359\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-6-1-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-6-1-1024x519.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-6-1-768x389.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-6-1-1536x778.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-6-1-1200x608.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-6-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Select &#8220;AWS service&#8221; for trusted entity and &#8220;EC2&#8221; for &#8220;Use case&#8221; as we are creating a role and attaching it our ec2 instance which allows it(instance) to access ECR, then click on &#8220;Next&#8221;.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1766 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-7-1-300x152.jpeg\" alt=\"\" width=\"713\" height=\"361\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-7-1-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-7-1-1024x519.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-7-1-768x389.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-7-1-1536x778.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-7-1-1200x608.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-7-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Search for policy <strong>AmazonEC2ContainerRegistryFullAccess\u00a0<\/strong> , select it and click on &#8220;Next&#8221;.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1767 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-8-1-300x152.jpeg\" alt=\"\" width=\"721\" height=\"365\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-8-1-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-8-1-1024x520.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-8-1-768x390.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-8-1-1536x780.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-8-1-1200x609.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-8-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Give a name to your role, scroll down and click on create role.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1768 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-9-1-300x154.jpeg\" alt=\"\" width=\"712\" height=\"365\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-9-1-300x154.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-9-1-1024x524.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-9-1-768x393.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-9-1-1536x786.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-9-1-1200x614.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-9-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now assign the Role created to the ec2 instance in which you have docker image, go the ec2 instances select the instance and click on &#8220;Actions&#8221; and click on &#8220;Modify IAM role&#8221; under &#8220;Security&#8221;.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1771 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-10-2-300x152.jpeg\" alt=\"\" width=\"707\" height=\"358\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-10-2-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-10-2-1024x520.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-10-2-768x390.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-10-2-1536x780.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-10-2-1200x609.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-10-2.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>select the role you have created with <strong>AmazonEC2ContainerRegistryFullAccess\u00a0<\/strong>and click on &#8220;Update IAM Role&#8221;<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1772 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-11-1-300x152.jpeg\" alt=\"\" width=\"707\" height=\"358\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-11-1-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-11-1-1024x518.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-11-1-768x388.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-11-1-1536x777.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-11-1-1200x607.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-11-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now the ec2 instance has full access to Amazon ECR,<\/p>\n<h3><strong>Step-4: Authenticate with ECR repository<\/strong><\/h3>\n<p>To push Image to ECR you need to authenticate with it, we will use an AWS CLI command to authenticate with the registry, install AWS cli on your ec2 instance using below commands<\/p>\n<pre>curl \"https:\/\/awscli.amazonaws.com\/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\r\nsudo yum install unzip -y\r\nunzip awscliv2.zip\r\nsudo .\/aws\/install\r\n<\/pre>\n<p>Check your installation by executing below given command<\/p>\n<pre>aws --version<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-1774 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-12-300x23.jpeg\" alt=\"\" width=\"743\" height=\"57\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-12-300x23.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-12-1024x77.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-12-768x58.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-12-1536x115.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-12-1200x90.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-12.jpeg 1612w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>Note<\/strong>:\u00a0 if you are using your local machine refer this <a href=\"https:\/\/www.checkmateq.com\/blog\/wp-admin\/post.php?post=307&amp;action=edit\" target=\"_blank\" rel=\"noopener\">blog<\/a> to configure your AWS CLI.<\/p>\n<p>Now go to your Repository and copy its URI.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1777 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-15-300x153.jpeg\" alt=\"\" width=\"686\" height=\"350\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-15-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-15-1024x521.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-15-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-15-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-15-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-15.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Use below command to retrieve the authentication details and authenticate with your registry<\/p>\n<pre>aws ecr get-login-password --region <strong>ap-south-1<\/strong> | docker login --username AWS --password-stdin <strong>109714447277.dkr.ecr.ap-south-1.amazonaws.com<\/strong><\/pre>\n<p><strong>ap-south-1<\/strong> is the AWS region where I created my ECR, give your region here.<\/p>\n<p><strong>109714447277.dkr.ecr.ap-south-1.amazonaws.com\u00a0<\/strong>is my ECR URI, exclude ECR name from the end of ECR URI, give your URI here.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1776 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-14-300x29.jpeg\" alt=\"\" width=\"1034\" height=\"100\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-14-300x29.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-14-1024x98.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-14-768x74.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-14-1536x147.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-14-1200x115.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-14.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<h3><strong>Step-5: Build Docker Image and Push it to ECR<\/strong><\/h3>\n<p>Go to the directory where your Docker file is located, use the command given below to build a docker image<\/p>\n<pre>docker build -t <strong>apache-prac-ecr<\/strong> .<\/pre>\n<p><strong>apache-prac-ecr<\/strong> is the name of image and &#8220;.&#8221; indicates that Docker file is in same path.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1792 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-18-300x164.jpeg\" alt=\"\" width=\"689\" height=\"377\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-18-300x164.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-18-1024x561.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-18-768x421.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-18-1536x842.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-18-1200x658.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-18.jpeg 1803w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Tag your image after the build is completed successfully.<\/p>\n<pre>docker tag apache-prac-ecr:<strong>latest<\/strong> 109714447277.dkr.ecr.ap-south-1.amazonaws.com\/aksshaay-prac-ecr:<strong>latest<\/strong><\/pre>\n<p>By giving a tag for your Docker image it will be easy to pull and run images, in the above command latest is the tag.<\/p>\n<p>Finally push the Docker image to the repository, use command <strong>docker push <\/strong>along with ECR URI and repo name as shown below.<\/p>\n<pre>docker push 109714447277.dkr.ecr.ap-south-1.amazonaws.com\/aksshaay-prac-ecr:latest<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-1790 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-16-300x69.jpeg\" alt=\"\" width=\"734\" height=\"169\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-16-300x69.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-16-1024x236.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-16-768x177.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-16-1536x354.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-16-1200x277.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-16.jpeg 1898w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>below is the (snapshot from my AWS web console) details of uploaded image to my repository.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1825 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-20-300x152.jpeg\" alt=\"\" width=\"698\" height=\"354\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-20-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-20-1024x518.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-20-768x389.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-20-1536x778.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-20-1200x608.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/ecr-20.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Author Information<\/strong><\/p>\n<p>This blog is written by Checkmate DevOps team, you can connect with our Technical consultant to hire experienced <a href=\"https:\/\/www.checkmateq.com\/devops-engineering\">DevOps engineer<\/a> to manage entire Infrastructure technical stack.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why do we need docker container? Did you ever listened of a problem from your teammates that the code you have shared is not working but it was perfectly alright when you used it, ok ,let&#8217;s understand this with a practical scenario , assume you are a developer in your organization ,you have developed a &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/build-docker-image-to-aws-ecr\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Build a Docker Image and Push it To AWS ECR?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":1843,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[65,3,2,64,66,7,22,59,11,63,23,24],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/1644"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=1644"}],"version-history":[{"count":46,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/1644\/revisions"}],"predecessor-version":[{"id":4294,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/1644\/revisions\/4294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/1843"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=1644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=1644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=1644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}