How to Push a Docker Image to the AWS ECR
In this tutorial, I will show how to push an image to AWS. All the steps shown below were performed on Ubuntu 18 with Docker version 19.03.6 and AWS CLI version 2 installed.
1. Create the AWS ECR repository
In the AWS console go to the AWS ECR page. Click the “Create repository” button.
Then chouse visibility of your repository. I leave it as “private”, so it will be managed by IAM and repository policy permissions and won't be accessible to the public. Then fill up the name and click Create a repository on the bottom of the page.
An empty repository has been created!
2. Prepare the image to be pushed.
In this example, I will push the image of a simple Node.js app that listens on port 8080 and displays its host/container name. The source code you can check here. The root directory has a Dockerfile. We will use it to build an image. Before pushing the image to the repository we need to tag it with the repository URL. In the root directory in the terminal run commands:
The result will be like this:
3. Authenticate the Docker CLI to your AWS ECR
Now we need to authenticate the Docker CLI to AWS ECR.
For this command to execute successfully you have to have your AWS credentials stored in the credentials file and your IAM principal has to have the necessary permission. Make sure you use the right region, as it is a common mistake.
This command retrieves an authentication token using the GetAuthorizationToken API and then redirects it using the pipe (|) to the login command of the container client, the Docker CLI in my case. The authorization token is valid for 12 hours.
4. Push the image to AWS ECR
To push the image to the AWS ECR we will execute the following command:
As you can see, to push the image I've used the tag created in step 2 of this tutorial.
Now the image is in my repository created in step 1.
Summary
This tutorial shows how in easy steps we can push any image to the AWS ECR repository. Thank you for reading! If you have any questions or suggestions, please feel free to write me on my LinkedIn account.
More content at plainenglish.io