Sub topic 5
Building, tagging, pushing, and pulling an image
This chapter assumes you can build and run a Docker image, you have the AWS CLI configured, and you already created the my-app repository and logged Docker in to the registry in the previous chapters. Here you take a local image to ECR and back.
How an ECR image name is built
An ECR image reference has the form <account-id>.dkr.ecr.<region>.amazonaws.com/<repository>:<tag>. Docker decides where to push by reading the host part of the name. If the name has no registry host, Docker pushes to Docker Hub. For ECR, the tag step is therefore required.
Build, tag, and push
Build the image with a local name, add a second name that contains the full repository URI, then push that name.
docker build -t my-app:1.0.0 .
docker tag my-app:1.0.0 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0docker tag does not copy any data. It adds a second name that points to the same image. During the push, Docker uploads each layer the registry does not already have, then uploads the manifest. Layers that already exist in the repository are skipped, so a second push of a similar image is fast.
If you build on an Apple silicon laptop and run on x86 servers, add --platform linux/amd64 to docker build. Otherwise the image runs only on ARM hosts.
List what you pushed
Ask ECR for the tags and digests it holds:
aws ecr describe-images \
--repository-name my-app \
--region us-east-1 \
--query 'imageDetails[].{tags:imageTags,digest:imageDigest,pushed:imagePushedAt}'Pull by tag or by digest
Pulling by tag is convenient. Pulling by digest is exact, because a digest identifies one specific manifest and can never refer to different content. Replace <digest> with a value from the listing above.
docker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0
docker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app@sha256:<digest>Permissions involved
Your IAM identity needs these actions on the repository:
| Task | Actions |
|---|---|
| Log in | `ecr:GetAuthorizationToken` (on all resources) |
| Pull | `ecr:BatchGetImage`, `ecr:GetDownloadUrlForLayer`, `ecr:BatchCheckLayerAvailability` |
| Push | `ecr:BatchCheckLayerAvailability`, `ecr:InitiateLayerUpload`, `ecr:UploadLayerPart`, `ecr:CompleteLayerUpload`, `ecr:PutImage` |
Common errors
no basic auth credentials: Docker has no valid login for that registry host. Log in again, and check that the account ID and Region in the login command match the image name.name unknown: The repository with name 'my-app' does not exist: Create the repository first. ECR does not create repositories on push. Also check that you are using the right Region.deniedornot authorized: The IAM identity lacks one of the actions in the table, or a repository policy blocks it.tag invalid: The image tag '1.0.0' already exists: The repository has tag immutability on, so the tag cannot be reused. Push a new version tag. Retagging the same content under a new tag is fine.
Exercise
You push my-app:1.0.0 to a repository with tag immutability enabled, fix a bug, rebuild, and push again with the same tag. What happens, and what should you do?
Show answer
The second push is rejected because the tag already exists, and the repository still holds the original image. Tag the rebuilt image with a new unique tag, such as 1.0.1 or a commit hash, and push that tag.