0Pricing
AWS Solutions Architect · Lesson

ECR: Storing and Pulling Container Images

Push Docker images to Amazon ECR, apply lifecycle policies to trim old images, and pull images securely into ECS tasks.

ECR: Storing and Pulling Container Images is a free AWS Solutions Architect lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Amazon ECR?

Amazon Elastic Container Registry (ECR) is a fully managed Docker-compatible container image registry. It stores, manages, and deploys container images securely within AWS. ECR eliminates the need to operate your own registry infrastructure and integrates natively with ECS, EKS, Lambda (container images), and CodeBuild. Images stored in ECR are replicated for high availability and can be scanned for security vulnerabilities automatically.

Private vs Public ECR Repositories

ECR supports two repository types: Private repositories in Amazon ECR require AWS authentication to pull images—ideal for proprietary application images. Access is controlled by IAM policies and repository resource policies. Public repositories on ECR Public Gallery (public.ecr.aws) allow unauthenticated pulls from anywhere on the internet—ideal for open-source base images, public tools, and AWS-provided images (Lambda base images, ECS-optimised AMI images). Public repositories have a free pull allowance with rate limits for unauthenticated requests.

# Create a private ECR repository
aws ecr create-repository \
  --repository-name 'myapp/backend' \
  --image-scanning-configuration scanOnPush=true \
  --image-tag-mutability IMMUTABLE

Authenticating Docker to ECR

To push or pull private ECR images, Docker must authenticate using AWS credentials. Use aws ecr get-login-password to obtain a temporary authentication token and pipe it to docker login. The token is valid for 12 hours. In CI/CD pipelines, refresh the token before each push. ECS task agents, Lambda, and EKS worker nodes authenticate to ECR automatically using their IAM role—no manual login needed for runtime pulls in AWS environments.

# Authenticate Docker CLI to ECR
aws ecr get-login-password --region us-east-1 | \
  docker login \
    --username AWS \
    --password-stdin \
    123456789012.dkr.ecr.us-east-1.amazonaws.com

Pushing Images to ECR

The standard workflow to push an image to ECR: (1) Build the Docker image locally; (2) Tag the image with the ECR repository URI (format: account.dkr.ecr.region.amazonaws.com/repo-name:tag); (3) Push the tagged image. ECR stores each image layer separately and deduplicates common layers across images, reducing storage costs. Using immutable tags prevents overwriting a tag, ensuring deployed image versions are stable and auditable.

# Build, tag, and push
docker build -t myapp/backend .
docker tag myapp/backend \
  123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp/backend:v1.2.3
docker push \
  123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp/backend:v1.2.3

Image Tag Mutability

IMMUTABLE tags prevent a tag from being overwritten with a different image. When tag mutability is IMMUTABLE, pushing a new image to an existing tag fails—you must use a new tag. This is a best practice for production: it ensures that :v1.2.3 always refers to exactly the same image digest, enabling reliable rollbacks and auditing. Use MUTABLE tags only for development branches or the :latest convenience tag in non-production environments.

Image Scanning for Vulnerabilities

Enable scan on push to automatically scan images for known CVEs when they are pushed. ECR uses the Amazon Inspector integration (enhanced scanning) or the built-in Basic Scanning powered by the open-source Clair scanner. Enhanced scanning provides continuous scanning (not just on push) and covers OS packages and language-specific packages (Node.js, Python, Java). Review scan findings in the ECR console or via EventBridge notifications to your security team.

# Enable enhanced scanning for a registry
aws ecr put-registry-scanning-configuration \
  --scan-type ENHANCED \
  --rules '[{"repositoryFilters": [{"filter": "*", "filterType": "WILDCARD"}], "scanFrequency": "CONTINUOUS_SCAN"}]'

Lifecycle Policies to Manage Image Costs

ECR charges for storage per GB per month. Without management, old images accumulate indefinitely, increasing costs. Lifecycle policies automatically expire and delete images based on rules: for example, keep only the last 10 tagged releases, or delete untagged images older than 7 days. Lifecycle policies run daily. This is especially important in CI/CD pipelines that push new images on every commit, which can accumulate hundreds of images per week.

aws ecr put-lifecycle-policy \
  --repository-name 'myapp/backend' \
  --lifecycle-policy-text '{
    "rules": [
      {
        "rulePriority": 1,
        "description": "Keep last 10 tagged images",
        "selection": {"tagStatus": "tagged", "tagPrefixList": ["v"], "countType": "imageCountMoreThan", "countNumber": 10},
        "action": {"type": "expire"}
      },
      {
        "rulePriority": 2,
        "description": "Delete untagged images after 7 days",
        "selection": {"tagStatus": "untagged", "countType": "sinceImagePushed", "countUnit": "days", "countNumber": 7},
        "action": {"type": "expire"}
      }
    ]
  }'

Cross-Account Image Access

To pull images from an ECR repository in Account A into ECS/EKS/Lambda in Account B, configure a repository policy (resource-based policy) on the ECR repository in Account A granting the IAM principal from Account B permission to call ecr:GetDownloadUrlForLayer, ecr:BatchGetImage, and ecr:BatchCheckLayerAvailability. This eliminates the need to copy images between accounts and enables centralised image management with distributed consumption.

ECR Replication for Multi-Region Deployments

ECR replication automatically copies images from your primary registry to registries in other AWS Regions (and optionally other accounts). When ECS tasks in another Region pull an image, they pull from the local regional registry—reducing data transfer costs and image pull latency. Configure replication rules at the registry level specifying which repositories to replicate and which destination regions and accounts. Replication is asynchronous and near-real-time.

aws ecr put-replication-configuration \
  --replication-configuration '{
    "rules": [{
      "destinations": [
        {"region": "eu-west-1", "registryId": "123456789012"},
        {"region": "ap-southeast-1", "registryId": "123456789012"}
      ],
      "repositoryFilters": [{"filter": "prod/*", "filterType": "PREFIX_MATCH"}]
    }]
  }'

ECR Encryption

ECR encrypts images at rest using server-side encryption. By default it uses an AWS-managed key (AWS_MANAGED_KEY). For additional control and auditability, configure a customer-managed KMS key (CMK). With a CMK, you control key rotation, can revoke access by disabling the key, and can audit all decryption operations in CloudTrail. Enable KMS encryption when regulatory compliance (PCI-DSS, HIPAA, FedRAMP) requires customer-controlled encryption keys.

Using ECR with ECS in CI/CD Pipelines

A complete CI/CD pipeline for ECS with ECR: (1) Developer pushes code to Git; (2) CodeBuild builds the Docker image and pushes it to ECR with a commit SHA tag; (3) CodePipeline updates the ECS task definition with the new image URI; (4) ECS performs a rolling update of the service, pulling the new image from ECR. ECS tasks use the Task Execution Role to authenticate to ECR automatically—no credentials need to be managed in the pipeline for runtime pulls.

Quick Check

Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.

Lesson Recap

In this lesson you learned: ECR private repositories store container images with IAM-controlled access, immutable tags for production stability, and automatic vulnerability scanning with Inspector, Lifecycle Policies automatically expire old and untagged images to control storage costs in active CI/CD environments, and ECR Replication copies images to other Regions and accounts for multi-region deployments at lower pull latency. Next up we explore ECS Service Auto Scaling and load balancing integration.

Frequently asked questions

Is the “ECR: Storing and Pulling Container Images” lesson free?

Yes — the full text of “ECR: Storing and Pulling Container Images” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.

What will I learn in “ECR: Storing and Pulling Container Images”?

Push Docker images to Amazon ECR, apply lifecycle policies to trim old images, and pull images securely into ECS tasks. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AWS Solutions Architect?

No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ECR: Storing and Pulling Container Images” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AWS Solutions Architect lesson?

Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. ECS Clusters, Task Definitions, and Services
  2. EC2 Launch Type vs Fargate
  3. ECR: Storing and Pulling Container Images
  4. ECS Service Auto Scaling and Load Balancing
← Back to AWS Solutions Architect