Open In App

How To Push A Docker Image To Amazon ECR?

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We go over how to submit a Docker image to the Amazon Elastic Container Registry (ECR) in this tutorial. By offering a safe, scalable registry for storing and distributing Docker images inside the AWS ecosystem, Amazon ECR streamlines container management. To upload and maintain your containerized applications in Amazon ECR with ease, simply follow these instructions.

What is Docker?

Docker is a platform where it is easier to make, run, and deploy applications using containers. It simplifies the process of building, running, and deploying applications by providing a consistent environment across different platforms and infrastructure.

What is Amazon ECR?

Amazon Elastic Container Registry (ECR) is a fully managed Docker container service registry provided by Amazon Web Services (AWS). It can store, manage, and deploy container images securely. It is a reliable platform for storing and managing containerized applications.

How to push a Docker image to Amazon ECR?

Prerequisites

  • Docker Hub account
  • Base knowledge of Docker
  • AWS account
  • AWS CLI installed
  • Configure AWS
  • Docker CLI

Step 1: Build a Docker image

To demonstrate how to push a Docker image into ECR, we’ll make a simple Docker image. Create a simple Docker image using the following link given below.

Link:- https://www.geeksforgeeks.org/create-docker-image/

After creating the docker image run the image and access it through localhost.

Step 2: Creating the AWS ECR repository

Log in to the AWS console and go to the AWS ECR. Then click Get Started to create a repository.

ECR-getstarted

ECR get started

In the General settings , set the visibility settings to Private. Then set the Repository name according to your choice , Here we are naming it as ” gfg-ecr ” . Leave other settings as it is.

Name the ECR

Name the ECR

Click Create repository on the bottom.

Create repository

Create repository

An empty repository now created named “gfg-ecr ” .

Repository created

Repository created

Step 3: Set up the image to be pushed

Here we will push a simple dokcer image of index.html that listens on port 8080. The root directory has a Dockerfile. We will use it to build an image.

The Dockerfile contains the following commands:

FROM nginx:alpine
COPY index.html /usr/share/nginx/html
EXPOSE 80

The index.html file contains:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>How to create docker image</title>
</head>

<body>
<H1>Hey geeks to create a docker image you have to write a Dockerfile and then build it using
docker command .
</H1>
</body>

</html>

Index code

Step 4: Build the container image and deploy locally

The container image will be built using the command below.

docker image build -t <image_name> .

The dot indicates that the Dockerfile image resides in the current directory.

After the build is finished deploy your container locally in port 8080 with the following command

docker run -d --name <container_name> -p 8080:80 <image_name>

Step 5: Push image to the Amazon ECR repository

We need to authenticate the Docker client to the registry. To proceed with the authentication, we would run a get-login-password command in the CLI to retrieve the token and then pass it to the docker login command.

(Get-ECRLoginCommand).Password | docker login --username AWS --password-stdin 046818881351.dkr.ecr.eu-north-1.amazonaws.com

After the build completes, tag your image so you can push the image to this repository:

docker tag gfg-ecr:latest 046818881351.dkr.ecr.eu-north-1.amazonaws.com/gfg-ecr:latest

Run the following command to push this image to your newly created AWS repository:

docker push 046818881351.dkr.ecr.eu-north-1.amazonaws.com/gfg-ecr:latest
Push commands in the ECR

Push commands in the ECR

Pushing A Docker Image Amazon ECR – FAQ’s

How to push Docker image in ECR?

To push a Docker image to Amazon ECR, use the docker push command along with the ECR repository URI. Authenticate Docker with ECR using the aws ecr get-login-password command, and then push the image using docker push <ECR-repository-URI>.

How do I run a Docker image in ECR?

To run a Docker image from Amazon ECR, use the docker run command, providing the ECR repository URI as the image name. Ensure you’ve authenticated Docker with ECR using aws ecr get-login-password before running the image.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads