crunchcrunch

crunchcrunch

How to push a Docker image to Amazon ECR repository

How to push a Docker image to Amazon ECR repository

In this tutorial, i am going to show you how to push a Docker image to Amazon ECR repository.

Create the repository

Connect to you AWS account and go to Amazon ECR (Amazon Elastic Container Registry) on the console.

Capture d'écran 2021-03-27 08:49:47.png

Click on get started and choose a name for your repository (i chose a private one)

Capture d'écran 2021-03-27 08:52:33.png

Then click on Create Repository

Capture d'écran 2021-03-27 08:54:49.png

If you click on your repository, you will see that there is not image in it.

Capture d'écran 2021-03-27 08:55:51.png

Get a Docker image

Now we need a docker image.

I am going to use this project : Express app

Feel free to clone and use it if you don't have a Docker image around.

Build the image

docker build -t my-ecr-test .
Step 1/7 : FROM node:10-alpine
 ---> ef405b0ed7aa
Step 2/7 : WORKDIR /home/app
 ---> Using cache
 ---> 4da306ecf643
Step 3/7 : COPY package*.json ./
 ---> Using cache
 ---> b7978055117e
Step 4/7 : RUN npm install
 ---> Using cache
 ---> f163bf60a9b5
Step 5/7 : COPY app .
 ---> Using cache
 ---> 386334c6d2c7
Step 6/7 : EXPOSE 8080
 ---> Using cache
 ---> 9b2cb4527a38
Step 7/7 : CMD [ "node", "server.js" ]
 ---> Using cache
 ---> f174a56b12df
Successfully built f174a56b12df
Successfully tagged my-ecr-test:latest

Now tag the image to push it to the repository

docker tag my-ecr-test:latest <id_of_your_repository>.dkr.ecr.eu-west-1.amazonaws.com/my-ecr-test:latest

Now your image is ready to be pushed to the repository

docker images
REPOSITORY                                                  TAG         IMAGE ID       CREATED        SIZE
<id_of_your_repository>.dkr.ecr.eu-west-1.amazonaws.com/my-ecr-test    latest      f174a56b12df   18 hours ago

Authentication to the registry and push

Last thing to do before pushing the image, you need to get an authentification token for your Docker client to be able to push to the AWS repository.

To do that, use the CLI:

aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin <id_of_your_repository>.dkr.ecr.eu-west-1.amazonaws.com
...
Login Succeeded

Now last step, push the image :

docker push <id_of_your_repository>.dkr.ecr.eu-west-1.amazonaws.com/my-ecr-test:latest
The push refers to repository [<id_of_your_repository>.dkr.ecr.eu-west-1.amazonaws.com/my-ecr-test]
a2ca228cd840: Pushed 
42b0264eb570: Pushed 
b6bcadc8bd46: Pushed 
8ed941da8d6d: Pushed 
6bb8c71be1d4: Pushed 
c08b6038b679: Pushed 
dbfdce5f4ef9: Pushed 
b3e46aac4e11: Pushed 
latest: digest: sha256:05b5082a2cdb11be1201d0497f07578286e86adc227537e530ed1d36e2c996f8 size: 1993

If you go back to your console and hit refresh, you'll see your image in your repository !

Capture d'écran 2021-03-27 09:11:31.png

Congrats ! :)

Let me know if you have any questions in the comments.

#aws#docker#docker-images
 
Share this