Push Docker Image to AWS ECR from CLI/Powershell
Published: 2023-09-13 17:11:09

Pre-requisites

1. [Install AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and configure it
2. IAM user with permission to ECR or AWS root account (Please avoid it for security purposes) 
3.  (basic user) 

## Create an ECR Repository

1. Login to AWS console: 
2. Create ECR Repository: 
` Type "ECR" in search box -> Amazon ECR -> Repositories -> Create repository `
3. Enter name of repository 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uizd89pquwfznkxaob74.jpg)

4. Verify private repository is created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ba156gks3tj6tclhd94a.jpg)

5. Click on Repo-> "View Push Command" button 
` $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/my-repo
##AWS_ACCOUNT_ID ~ 907060907920 $AWS_REGION ~ us-west-2 `

For MacOS/Linux
`
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
`

For Windows
`
(Get-ECRLoginCommand).Password | docker login --username AWS --password-stdin %AWS_ACCOUNT_ID%.dkr.ecr.%AWS_REGION%.amazonaws.com
`
If Windows user gets below error, just use MacOS/Linux method - To know more [click here](https://techsparx.com/software-development/aws/ecs/use-aws-cli.html) to get a workaround on windows error.

ERROR: 
``` 
 (Get-ECRLoginCommand).Password | docker login --username AWS --password-stdin 917165987323.dkr.ecr.us-east-1.amazonaws.com
Get-ECRLoginCommand : The term 'Get-ECRLoginCommand' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:2
+ (Get-ECRLoginCommand).Password | docker login --username AWS --passwo ...
+  ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-ECRLoginCommand:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
```
 

- After Successful Login to ECR
```
Login Succeeded

Logging in with your password grants your terminal complete access to your account.
For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/ 
```
- Create a simple and name it as 'Dockerfile'
> Note: in Windows create a text file and add in below code and save as "Dockerfile" including quotes(")

`FROM ubuntu:latest`
- Build Docker image via Dockerfile
` docker build -t jz-repo .`
```
PS C:\Users\Jasper\OneDrive\Documents\CodeRepo\aws_ecr> docker build -t jz-repo .
[+] Building 33.4s (6/6) FINISHED                                                                        docker:default
 => [internal] load build definition from Dockerfile                                                               0.1s
 => => transferring dockerfile: 55B                                                                                0.0s
 => [internal] load .dockerignore                                                                                  0.1s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                   2.9s
 => [auth] library/ubuntu:pull token for registry-1.docker.io                                                      0.0s
 => [1/1] FROM docker.io/library/ubuntu:latest@sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce  30.2s
 => => resolve docker.io/library/ubuntu:latest@sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3  0.1s
 => => sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054 1.13kB / 1.13kB                     0.0s
 => => sha256:b492494d8e0113c4ad3fe4528a4b5ff89faa5331f7d52c5c138196f69ce176a6 424B / 424B                         0.0s
 => => sha256:c6b84b685f35f1a5d63661f5d4aa662ad9b7ee4f4b8c394c022f25023c907b65 2.30kB / 2.30kB                     0.0s
 => => sha256:445a6a12be2be54b4da18d7c77d4a41bc4746bc422f1f4325a60ff4fc7ea2e5d 29.54MB / 29.54MB                  24.5s
 => => extracting sha256:445a6a12be2be54b4da18d7c77d4a41bc4746bc422f1f4325a60ff4fc7ea2e5d                          5.0s
 => exporting to image                                                                                             0.0s
 => => exporting layers                                                                                            0.0s
 => => writing image sha256:18f1928b30e0cc26fd9e768656be0c61f3fc5ee34fd78f1ae56b1694b9f43ce4                       0.0s
 => => naming to docker.io/library/jz-repo                                                                         0.0s

What's Next?
  View summary of image vulnerabilities and recommendations → docker scout quickview
```
- Tag Docker Image to push it to ECR repository
`docker tag jz-repo:latest 1234.dkr.ecr.us-west-1.amazonaws.com/my-repo:latest` 
- Push the Docker image to ECR repository
```
docker push 1234.dkr.ecr.us-west-1.amazonaws.com/my-repo:latest

The push refers to repository [1234.dkr.ecr.us-west-1.amazonaws.com/my-repo]
dc0585a4b8b7: Pushed
latest: digest: sha256:969ad889a83b2444fab9bf5be2f781b855b060163de1d9ef38bd3cbbde21f8a7 size: 529
```
- verify Pushed Image

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04g1jtaof50ylhzn0l64.png)