Home » How to Push Image to ECR: A Complete AWS CLI Guide

How to Push Image to ECR: A Complete AWS CLI Guide

Push Image using CLI

Introduction

In modern containerized deployments, storing custom Docker images in a secure, private registry is critical for multi-tenant isolation. Amazon Elastic Container Registry (ECR) serves as the primary component for storing these artifacts within AWS. However, successfully executing your first remote push sequence using the AWS CLI requires more than just running simple terminal scripts. You must actively manage IAM permissions boundaries, handle temporary token lifecycles that expire every 12 hours, and ensure your container engine local architecture aligns perfectly with your remote ECR registry URI schemas. This guide shifts away from basic tutorial copy-pasting to establish a production-grade container push pipeline with proper credential handling and image tags configurations.

What I am working with

Before jumping in, here’s my setup so you can compare against yours if something behaves differently

  • EC2 instance (or local machine) with Docker installed
  • AWS CLI v2 configured with an IAM user that has ECR permissions

Step 1: Know your account ID and region before you start

It’s the one that actually confuses people the most. Every ECR image URI is built using your AWS account ID and the region you’re working in, so let’s grab both first instead of guessing later.

  • To get your account ID
Multi Copy Code Blocks
bash

aws sts get-caller-identity
    
  • This returns something like

That Account value a 12-digit number is what I’ll be plugging into every ECR command from here on so copy your account ID we will use later.

  • To check which region your CLI is currently pointed at:
Multi Copy Code Blocks
bash

aws configure get region
    

Note: write your actual account ID and region here once you run these you’ll be using them literally everywhere below

Step 2: Install and Setup Docker on your instances

If Docker isn’t installed yet on your EC2 instance, here’s what I run

  • For Amazon Linux
Multi Copy Code Blocks
bash

sudo yum update -y
sudo amazon-linux-extras enable docker
sudo yum install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user

    
  • For Ubuntu Linux
Multi Copy Code Blocks
bash

sudo apt-get update -y
sudo apt-get install docker.io -y 
sudo systemctl start docker 
sudo usermod -aG docker ubuntu
    

Basically above command are update your Linux package manager repositories to ensure core security patches are synced with upstream distribution mirrors. Deploy the underlying Docker daemon and engine files directly onto the cloud instance volume. Start the Docker daemon process loop and configure the system manager systemctl to persist and automatically restart the runtime engine upon unexpected instance reboots.

By default, the Docker daemon binds to a Unix socket owned exclusively by the root user. To execute container commands without constantly prefixing sudo map your default cloud user context to the engine group space.

Note: To force the terminal to register these modified permissions without closing your SSH session, run newgrp docker immediately.

Step 3: Create a project folder and a simple index.html

Create and open Dockerfile

Multi Copy Code Blocks
bash

mkdir my-app && cd my-app
sudo vi Dockerfile 
    
  • After run this command you get one folder and create a file named as Dockerfile. After create a Dockerfile paste on your file given below:
Multi Copy Code Blocks
bash

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

Create a index.html

  • Now create a simple index.html file to run container.
Multi Copy Code Blocks
bash

cd my-app
echo "<h1> Hello From Docker Container! Welcoem to AWS Community builder </h1>" > index.html
    

Step 4: Building the image from Dockerfile

Multi Copy Code Blocks
bash

docker build -t my-first-app .
    
  • Once it’s built, confirm it’s sitting locally
Multi Copy Code Blocks
bash

docker images
    
  • If you want to sanity-check it actually works before pushing anywhere, run it locally and curl it
Multi Copy Code Blocks
bash

docker run -d -p 8080:80 my-first-app
curl localhost:8080
    

Step 5 : Creating the ECR repository through CLI

Before create ECR you have to install AWS CLI on your local machine if you don’t have AWS CLI or don;t know how to install so check out this link : how to install and setup AWS CLI on your local terminal.

  • Now that Docker’s confirmed working, let’s create somewhere on AWS to actually push this to
Multi Copy Code Blocks
bash

aws ecr create-repository --repository-name my-first-app --region us-east-1
    
  • That repositoryUri in the response is the thing you’ll need in a second, so don’t close that terminal window yet.

Notice that’s literally <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/<repo-name> the exact account ID and region you grabbed in Step 1. This is the piece that trips people up because it looks like some random string, but it’s not random at all.

Step 6: Authenticate Docker to Your Private Amazon ECR Registry

IAM Configuration

Before pushing artifacts, your local container engine needs authentication tokens. Use the AWS CLI to retrieve a temporary password string that automatically invalidates after 12 hours, and pipe it directly via standard input into the Docker login interface.

  • Execute the following command (replace region and account details with your parameters):
Multi Copy Code Blocks
bash

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 484907xxxxx.dkr.ecr.us-east-1.amazonaws.com
    
  • Output

Note : If this throws an error expired token, wrong region, credentials not configured document the exact message and the fix here. This is exactly the kind of “real error” that makes this post worth reading over the AWS docs

Step 7: Compile the Docker Image Infrastructure Explicitly

Build Layer

Build your application blueprint locally using a specific architectural build parameter. Setting explicit tags early prevents tag collisions within your remote environment.

  • Compile image container using a unique application tag
Multi Copy Code Blocks
bash

docker build -t my-first-app:v1 .
    

Step 8: Apply the Target Remote Registry Namespace Tag

Artifact Management

  • Docker needs to know the exact destination node mapping. Alias your local image file to align with the complete Amazon ECR repository URI string.
Multi Copy Code Blocks
bash

docker tag my-first-app:latest 484907xxxxx.dkr.ecr.us-east-1.amazonaws.com/my-first-app:latest
    
  • Confirm the tag actually applied
Multi Copy Code Blocks
bash

docker images
    
  • Output

Step 9: Execute the Secure Ingress Remote Container Push

Registry Export

  • Transmit the compiled build layers over HTTPS into your private cloud data store. The engine will skip existing shared base layers and only stream differential custom blocks.
Multi Copy Code Blocks
bash

docker push 484907xxxxx.dkr.ecr.us-east-1.amazonaws.com/my-first-app:latest
    
  • Output look similar like this

Step 8 : Confirming it actually landed in ECR

verify

  • To verify your ECR you can check using CLI or you can also check from AWS management console.
Multi Copy Code Blocks
bash

aws ecr describe-images --repository-name my-first-app --region us-east-1
    
  • Output

What this actually cost me

Note: Fill this in after checking Billing ECR has a free tier for storage, but note here how much you actually used, image size, and whether you saw any charge at all

Step 9 : Cleaning up

Since I don’t like resources sitting around racking up charges, here’s how I tore it down after confirming everything worked

Multi Copy Code Blocks
bash

aws ecr delete-repository --repository-name my-first-app --region us-east-1 --force
docker rmi my-first-app:latest
docker rmi 4849075xxxx.dkr.ecr.us-east-1.amazonaws.com/my-first-app:latest
    

A few things I’d tell past-me

  • Run aws sts get-caller-identity before anything else half the “confusing” ECR URI stuff makes sense the moment you see your own account ID sitting right there
  • Don’t skip the usermod/newgrp docker step the permission errors before that fix are annoying
  • This particular run had zero errors end to end, which honestly felt a little suspicious given how often something usually breaks. If yours goes just as smooth, don’t assume you did something wrong.

Production Architecture Best Practices:

  • Image Tag Mutability Alert: In enterprise systems, configure your ECR Repository settings to “Tag Immutability: Enabled”. This architectural constraint prevents developers from overwriting production tags (like :latest) with unverified test code, preventing runtime drifts in Kubernetes/ECS clusters.
  • Security Ingress Scanning: Always enable “Scan on Push” inside your ECR configurations. Every layer pushed via the AWS CLI will immediately undergo a basic Vulnerability Assessment via Core CVE databases, alerting your infrastructure teams to unpatched base images before deployment.

Professional Troubleshooting: Common ECR Push Errors

Executing automated pushes across different authentication contexts often generates specific network or permission flags. Here is how to isolate and resolve them fast:

Error 1: IAM Authorization Token Failure

  • The Error Log:
Multi Copy Code Blocks
bash

An error occurred (AccessDeniedException) when calling the GetLoginPassword operation: User: arn:aws:iam::123456789012:user/deploy-user is not authorized to perform: ecr:GetAuthorizationToken on resource: *
    
  • The Fix: Your local AWS CLI identity profiles lack necessary permissions policy mappings. You must access the AWS IAM Console, select the specific user or instance role running the command, and attach a policy document containing at least the following baseline permissions block:
Multi Copy Code Blocks
JSON

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:InitiateLayerUpload",
                "ecr:UploadLayerPart",
                "ecr:CompleteLayerUpload",
                "ecr:PutImage"
            ],
            "Resource": "*"
        }
    ]
}
    

Error 2: Repository Target Does Not Exist

  • The Error Log
Multi Copy Code Blocks
bash

name unknown: The repository with name 'cloudwithyuvi-repo' does not exist in the registry with id '123456789012'
    
  • The Fix: Unlike Docker Hub, Amazon ECR will not auto-create a private repository space on the fly when you run docker push . The destination namespace target must be explicitly created beforehand. Generate the repository space via the AWS CLI by running this creation syntax:
Multi Copy Code Blocks
bash

aws ecr create-repository --repository-name my-app-repo --region us-east-1
    

Error 3: Broken Credential Helper Transport Drop

  • The Error Log:
Multi Copy Code Blocks
bash

docker: credentials store error: error getting credentials - err: exit status 1, out: `aws-ecr-login: target resource not found`
    
  • The Fix: This issue surfaces on Linux workstations where the local ~/.docker /config.json is mapped to an unconfigured third-party credentials helper credential system. Open your configuration parameters file
Multi Copy Code Blocks
bash

nano ~/.docker/config.json
    

If you’ve pushed images to ECR before did you hit the same login gotcha I did, or was your first stumbling block somewhere else entirely? Curious what tripped other people up.

One thought on “How to Push Image to ECR: A Complete AWS CLI Guide

Leave a Reply

Your email address will not be published. Required fields are marked *