Docker on Amazon Web Services
上QQ阅读APP看书,第一时间看更新

Publishing Docker images using the Docker CLI

You've already seen how to log into ECR, and building and tagging your Docker image is much the same as the local use case, except you need to specify the URI of your ECR repository when tagging the image.

The following example demonstrates building the todobackend image, tagging the image with the URI of your new ECR repository (for the actual URI of your repository), and verifying the image name using the docker images command:

> cd ../todobackend
> docker build -t 385605022855.dkr.ecr.us-east-1.amazonaws.com/docker-in-aws/todobackend .

Sending build context to Docker daemon 129.5kB
Step 1/25 : FROM alpine AS build
---> 3fd9065eaf02
Step 2/25 : LABEL application=todobackend
---> Using cache
---> f955808a07fd
...
...
...
Step 25/25 : USER app
---> Running in 4cf3fcab97c9
Removing intermediate container 4cf3fcab97c9
---> 2b2d8d17367c
Successfully built 2b2d8d17367c
Successfully tagged 385605022855.dkr.ecr.us-east-1.amazonaws.com/docker-in-aws/todobackend:latest
> docker images
REPOSITORY TAG IMAGE ID SIZE
385605022855.dkr.ecr.us-east-1.amazonaws.com/docker-in-aws/todobackend latest 2b2d8d17367c 99.4MB
Tagging an Image for ECR

Once you have built and tagged your image, you can push your image to ECR.

Note that to publish an image to ECR, you require various ECR permissions. Because you are using the admin role in your account, you automatically have all the required permissions. We will discuss ECR permissions in more detail later on in this chapter.

Because you have already logged into ECR, this is as simple as using the docker push command and referencing the name of your Docker image:

> docker push 385605022855.dkr.ecr.us-east-1.amazonaws.com/docker-in-aws/todobackend
The push refers to repository [385605022855.dkr.ecr.us-east-1.amazonaws.com/docker-in-aws/todobackend]
1cdf73b07ed7: Pushed
0dfffc4aa16e: Pushed
baaced0ec8f8: Pushed
e3b27097ac3f: Pushed
3a29354c4bcc: Pushed
a031167f960b: Pushed
cd7100a72410: Pushed
latest: digest: sha256:322c8b378dd90b3a1a6dc8553baf03b4eb13ebafcc926d9d87c010f08e0339fa size: 1787
Pushing an image to ECR

If you now navigate to the todobackend repository in the ECS console, you should see your newly published image appear with the default latest tag, as shown in the following figure. Notice that when you compare the built size of the image (99 MB in my example) with the size of the image stored in ECR (34 MB in my example), you can see that ECR stores the image in a compressed format, which reduces storage costs.

In terms of charges for using ECR, AWS charges for both data storage and data transfer out (that is, pulling a Docker image). See https://aws.amazon.com/ecr/pricing/ for more details.
Viewing ECR images