Amazon ECR Explained: How to Create a Repository and Push a Docker Image
You've built a Docker image locally and now need a secure, AWS-native place to store it before deploying to ECS or EKS. Amazon ECR (Elastic Container Registry) is that place — a fully managed container registry that eliminates the operational overhead of running your own registry while integrating natively with IAM, ECS, and other AWS services.
TL;DR
| Step | Action | Tool |
|---|---|---|
| 1 | Create an ECR repository | AWS Console / CLI |
| 2 | Authenticate Docker to ECR | AWS CLI + Docker |
| 3 | Tag your local image | Docker CLI |
| 4 | Push the image to ECR | Docker CLI |
| 5 | Verify the image in ECR | AWS Console / CLI |
What is Amazon ECR?
Amazon Elastic Container Registry (ECR) is a fully managed Docker-compatible container image registry hosted by AWS. Think of it as a private Docker Hub that lives inside your AWS account, governed by IAM, and co-located with your compute services.
Key characteristics:
- Private by default: Every repository is private unless you explicitly create a public repository via ECR Public.
- IAM-integrated: Access is controlled via IAM identity policies and ECR resource-based repository policies.
- Regional service: Repositories exist within a specific AWS region. Your ECS tasks should pull from the same region to avoid cross-region data transfer costs.
- Image scanning: ECR supports basic and enhanced vulnerability scanning via Amazon Inspector.
- Lifecycle policies: Automate cleanup of old or untagged images to control storage costs.
Architecture: Local Image → ECR → ECS
- Developer workstation builds a Docker image locally using
docker build. - The AWS CLI generates a temporary authorization token, allowing Docker to authenticate against the ECR registry endpoint.
- The image is tagged with the ECR repository URI and pushed via standard Docker push.
- Amazon ECR stores the image layers in its managed, encrypted storage.
- Amazon ECS (or EKS) pulls the image from ECR at task/pod launch time using the task execution role.
Prerequisites
- AWS CLI v2 installed and configured (
aws configure) with a profile that has ECR permissions. - Docker Desktop or Docker Engine installed and running locally.
- A Docker image already built locally (we'll use
my-app:latestas the example).
Required IAM Permissions
The IAM identity performing these steps needs, at minimum, the following permissions. Apply least privilege — scope the resource ARN to your specific repository once it exists.
🔽 Click to expand — IAM Policy (least privilege)
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CreateAndDescribeRepositories",
"Effect": "Allow",
"Action": [
"ecr:CreateRepository",
"ecr:DescribeRepositories"
],
"Resource": "*"
},
{
"Sid": "PushImageToRepository",
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken"
],
"Resource": "*"
},
{
"Sid": "PushImageLayers",
"Effect": "Allow",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:PutImage",
"ecr:DescribeImages"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-app"
}
]
}
Note: ecr:GetAuthorizationToken is an account-level action and does not support resource-level restrictions — it must use "Resource": "*".
Step 1: Create an ECR Repository
A repository is the namespace that holds all versions (tags) of a single container image. Create one per application or service.
aws ecr create-repository \
--repository-name my-app \
--region us-east-1 \
--image-scanning-configuration scanOnPush=true \
--encryption-configuration encryptionType=AES256
The response includes the repositoryUri — save this value. It follows the pattern:
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app
Replace 123456789012 with your actual AWS account ID and us-east-1 with your target region.
Step 2: Authenticate Docker to Your ECR Registry
ECR uses short-lived authorization tokens. The AWS CLI retrieves a token and pipes it directly into docker login.
aws ecr get-login-password \
--region us-east-1 \
| docker login \
--username AWS \
--password-stdin \
123456789012.dkr.ecr.us-east-1.amazonaws.com
On success, Docker outputs: Login Succeeded. The username is always the literal string AWS — this is not a placeholder.
Analogy: Think of this step like getting a temporary visitor badge at a secure office building. The AWS CLI is the front desk — it verifies your identity (IAM credentials) and issues a time-limited badge (the auth token). Docker uses that badge to enter the ECR registry. The badge expires, so you'll need a new one in each new terminal session or CI/CD pipeline run.
Step 3: Tag Your Local Image
Docker requires the image name to match the destination registry URI exactly. Tag your existing local image with the full ECR repository URI.
docker tag my-app:latest \
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:latest
This does not copy the image — it creates an alias pointing to the same image layers. You can verify with docker images.
Step 4: Push the Image to ECR
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:latest
Docker uploads only the layers that don't already exist in ECR (layer deduplication). For subsequent pushes of the same base image, only changed layers are transferred.
Step 5: Verify the Push
Confirm the image is stored in ECR using the CLI:
aws ecr describe-images \
--repository-name my-app \
--region us-east-1
You should see an entry with your image tag, digest, and push timestamp in the response.
Full Push Workflow at a Glance
Production Considerations
| Concern | Recommendation |
|---|---|
| Image tagging strategy | Avoid relying solely on :latest. Use immutable tags like Git commit SHAs or semantic versions for traceability. |
| Immutable tags | Enable imageTagMutability=IMMUTABLE on the repository to prevent tag overwrites. |
| Lifecycle policies | Define lifecycle rules to expire untagged images and retain only the last N tagged images, controlling storage costs. |
| Cross-account access | Use ECR repository policies to grant pull access to other AWS accounts (e.g., a separate production account). |
| ECS task execution role | Attach the AmazonECSTaskExecutionRolePolicy managed policy (or equivalent) to your ECS task execution role so ECS can pull images from ECR. |
| CI/CD pipelines | Use OIDC-based IAM roles (e.g., GitHub Actions OIDC) instead of long-lived access keys for pipeline authentication. |
Enabling Immutable Tags (Recommended)
aws ecr put-image-tag-mutability \
--repository-name my-app \
--image-tag-mutability IMMUTABLE \
--region us-east-1
Glossary
| Term | Definition |
|---|---|
| ECR Repository | A named namespace within ECR that stores all tagged versions of a single container image. |
| Repository URI | The full registry endpoint + repository name used as the Docker image name for push/pull operations. |
| Authorization Token | A short-lived credential issued by ecr:GetAuthorizationToken that Docker uses to authenticate with the ECR registry endpoint. |
| Image Digest | A SHA256 hash of the image manifest, providing a content-addressable, immutable reference to a specific image version. |
| Lifecycle Policy | A set of rules that automatically expire images in an ECR repository based on age or count criteria. |
Next Steps
- 📖 Amazon ECR User Guide — Official Documentation
- Configure an ECS Task Definition pointing to your new ECR image URI to deploy the container.
- Set up ECR lifecycle policies to automate image cleanup and manage storage costs.
- Integrate ECR scanning with Amazon Inspector for continuous vulnerability assessment of your images.
Comments
Post a Comment