How to Push Docker Images to Amazon ECR (Elastic Container Registry) - Private Repository

How to Push Docker Images to Amazon ECR (Elastic Container Registry) - Private Repository

ยท

3 min read

Introduction:

Amazon Elastic Container Registry (ECR) is a fully managed Docker container registry that makes it easy to store, manage, and deploy Docker container images. In this tutorial, we'll walk through the steps to push a Docker image to Amazon ECR. We'll start by setting up the necessary permissions and configurations, then proceed to push a Docker image to a private repository in Amazon ECR.

Step 1:

Set Up IAM User and Permissions Before we begin, ensure that you have an AWS account and appropriate permissions to create IAM users and access ECR.

1.1 Create an IAM User:

  • Sign in to the AWS Management Console.

  • Go to the IAM service.

  • Click on "Users" in the left sidebar.

  • Click on "Add user" and follow the prompts to create a new IAM user with programmatic access.

1.2 Attach Policies:

  • After creating the user, attach the "AmazonEC2ContainerRegistryFullAccess" policy to the user to grant permissions for managing ECR repositories.

Step 2:

Configure AWS CLI Now, configure the AWS Command Line Interface (CLI) with the credentials of the IAM user created in the previous step.

2.1 Install AWS CLI:

  • If you haven't already installed the AWS CLI, follow the installation instructions for your operating system from the AWS documentation.

  • refer this link ()

2.2 Configure AWS CLI:

  • Open a terminal or command prompt.

  • Run the command aws configure and enter the Access Key ID, Secret Access Key, default region, and output format as prompted.

Step 3:

Create a Private Repository in Amazon ECR Let's create a private repository in Amazon ECR where we'll push our Docker image.

3.1 Create Repository:

  • Run the command:

      aws ecr create-repository --repository-name REPO_NAME --region REGION_NAME
    

    Replace "REPO_NAME" with your desired repository name and "REGION_NAME" with the region name.

Step 4:

Push Docker Image to Amazon ECR Now, let's push our Docker image to the private repository we created in Amazon ECR.

4.1 Get Login Password:

  • Run the command:

      aws ecr get-login-password --region REGION_NAME
    

    Replace "REGION_NAME" with the region name.

  • This command retrieves an encrypted password needed for Docker login to the ECR.

4.2 Docker Login to ECR:

  • Run the command:

      docker login -u AWS -p <encrypted_password> <ECR_URI>
    

    Replace <encrypted_password> with the password obtained in the previous step and <ECR_URI> with the URI of your ECR repository.

4.3 Build Docker Image:

4.4 Tag Docker Image:

  • Run the command:

      docker tag <image_name> <ECR_URI>/<image_name>
    

    Replace <image_name> with your image name and <ECR_URI> with the URI of your ECR repository.

4.5 Push Docker Image:

  • Run the command:

      docker push <ECR_URI>/<image_name>
    

    This command pushes the Docker image to your Amazon ECR repository.

Step 5:

Verify Image in Amazon ECR Console Finally, verify that the Docker image has been successfully pushed to your Amazon ECR repository by checking the ECR console.

Conclusion: In this tutorial, we've covered the process of pushing a Docker image to Amazon ECR. By following these steps, you can efficiently manage and deploy Docker container images using Amazon ECR in your AWS environment.

Did you find this article valuable?

Support Navya A by becoming a sponsor. Any amount is appreciated!

ย