๐ณ Push Docker Images to Amazon ECR Using AWS CLI
Learn how to configure the AWS CLI, build a Docker image, and push it to Amazon Elastic Container Registry (ECR) in a few simple steps.
๐ง Prerequisites
- ✅ Docker installed and running
- ✅ AWS CLI installed (
aws --version
) - ✅ IAM user with programmatic access and ECR permissions
- ✅ Access Key and Secret Key for the IAM user
- ✅ An Amazon ECR repository already created (e.g.
731733338449.dkr.ecr.ap-south-1.amazonaws.com/myrepo
)
✅ Step 1: Configure AWS CLI
Run the command:
aws configure
Provide:
- AWS Access Key ID: Your IAM access key
- AWS Secret Access Key: Your IAM secret key
- Default region name:
ap-south-1
- Default output format:
json
This saves config in
~/.aws/credentials
and~/.aws/config
✅ Step 2: Authenticate Docker with Amazon ECR
aws ecr get-login-password --region ap-south-1 \
| docker login --username AWS --password-stdin 731733338449.dkr.ecr.ap-south-1.amazonaws.com
On success:
Login Succeeded
✅ Step 3: Create a Dockerfile (Optional – if no image exists)
Create a file called Dockerfile
:
# Use official Python image
FROM python:3.11-slim
# Set working directory
WORKDIR /app
# Add default command
CMD ["python3", "-c", "print('Hello from ECR!')"]
✅ Step 4: Build the Docker Image
docker build -t myimage:latest .
✅ Step 5: Tag the Image for ECR
docker tag myimage:latest 731733338449.dkr.ecr.ap-south-1.amazonaws.com/myrepo:latest
✅ Step 6: Push the Image to ECR
docker push 731733338449.dkr.ecr.ap-south-1.amazonaws.com/myrepo:latest
You’ll see output showing upload progress for each image layer.
✅ Step 7 (Optional): Verify Image in AWS Console
- Go to Amazon ECR Console
- Select your repository (
myrepo
) - Verify the image appears under the Images tab
๐ IAM Permissions Required
Attach this policy or use AWS managed policy AmazonEC2ContainerRegistryFullAccess
:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload"
],
"Resource": "*"
}
]
}
๐ Notes
- Use custom image tags like
v1.0
,staging
, orproduction
as needed - To pull the image from ECR:
docker pull 731733338449.dkr.ecr.ap-south-1.amazonaws.com/myrepo:latest
๐งน Delete an Image from Amazon ECR
You can delete an image from ECR by tag or digest using the AWS CLI.
✅ 1. Identify the Image Digest or Tag
List images in your repository:
For CMD, use double quotes only, no single quotes: and For PowerShell, use double quotes outside, single quotes inside:
aws ecr list-images --repository-name myrepo --region ap-south-1 --query "imageIds[*]" --output json
✅ 2. Delete Image by Tag
If you want to delete an image using a tag (e.g., latest
):
aws ecr batch-delete-image --repository-name myrepo --image-ids imageTag=latest --region ap-south-1
✅ 3. Delete Image by Digest
If you prefer to delete by digest (more precise):
aws ecr batch-delete-image --repository-name myrepo --image-ids imageDigest=sha256:your_digest_here --region ap-south-1
You can get the digest from the
list-images
command above.
✅ 4. Verify Deletion (Optional)
List images again to confirm deletion:
aws ecr list-images --repository-name myrepo --region ap-south-1
Comments
Post a Comment