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.
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.
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.
Click Create repository on the bottom.
An empty repository now created named “gfg-ecr ” .
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>
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
How To Push A Docker Image To Amazon ECR?
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.