DEV Community

Cover image for Push Docker image to AWS ECR
Jun Ueno
Jun Ueno

Posted on

Push Docker image to AWS ECR

Written with

Cameron Gibson

Versions

Homebrew 3.4.7
Nginx 1.21.6
Ngrok 3.0.2
AWS CLI 2.5.8

About AWS ECR

AWS ECR is a repository service like DockerHub.
You can create your own repository, either public or private.

How to

  1. Prepare Docker image, check it on local
  2. Push the image to ECR
  3. Pull ECR image and check it on local
  4. Delete ECR image

1. Prepare Docker image, check it on local

Make sure you already have a Docker image to push it.

If you're interested in React,
take a look at this article: Dockerizing React App

You don't have any images and just want to make a simple image,
check it out: Pull Nginx image and run the container

2. Push to AWS ECR

① Create your public image repository at ECR console
ECR console empty

② Click "View push command"
push command

③ Copy & run 1st command using AWS CLI, and login to AWS

aws ecr-public get-login-password --region <your region> | docker login --username AWS --password-stdin public.ecr.aws/~~~

Login Succeeded
Enter fullscreen mode Exit fullscreen mode

※ Skip 2nd command, we already have the image on local.

④ Copy & run the 3rd command, and tag your image to push to ECR

docker tag <your local image name>:latest public.ecr.aws/~~~~~/<your ECR repository name>:latest
Enter fullscreen mode Exit fullscreen mode

Tagged image

⑤ Copy & run the 4th command, and push the image to ECR

$ docker push public.ecr.aws/~~~~~/<your ECR repository name>:latest

# output
The push refers to repository [public.ecr.aws//~~~~~/<your ECR repository name>]
6532d1bd92d7: Pushing [========================================>          ]    181MB/223.4MB
1bfe2f2c209c: Pushing [==============================>                    ]  156.5MB/259.2MB
5bc57cb39f11: Pushed 
1f63745992bb: Pushed 
fea31d3e0c85: Pushed 
0fc8a3e8b32a: Pushed 
99307ceff565: Pushed 
5cc685c4cd61: Pushed 
6fd97e423126: Pushed 
ca58f1c44290: Pushing [==================>                                ]  188.3MB/510.5MB
957a6eed8d1f: Pushing [==========================================>        ]  123.8MB/145.5MB
85fe00380881: Pushing [==================================================>]  17.87MB
5d253e59e523: Waiting 
b9fd5db9c9a6: Waiting 
Enter fullscreen mode Exit fullscreen mode

⑥ Go back to ECR console
Click your repository name and confirm you successfully push the image to ECR
Image pushed to ECR

3. Pull ECR image and check it on local

① Delete the image you tagged

docker rmi public.ecr.aws/~~~~~/<your ECR repository name>:latest
Enter fullscreen mode Exit fullscreen mode

② Copy the URI of the image you pushed at ECR console and pull the image
The image URI must be exactly the same, but it's better to know where you can check the image URI.
Check the URI at ECR console

$ docker pull <URI you copied at ECR console>

# output
latest: Pulling from ~~~~~
Digest: sha256:~~~~~~~~~~~~~~~~~~~~
Status: Downloaded newer image for <URI you copied at ECR console>
<URI you copied at ECR console>
Enter fullscreen mode Exit fullscreen mode

③ Run the container you pulled from ECR

docker run -p 3001:3000 <URI you copied at ECR console>
Enter fullscreen mode Exit fullscreen mode

③ Confirm you successfully run the container
Access to http://localhost:3001

localhost3001

Thank you

I hope this helps.
Thank you for reading.

Top comments (0)