Sub topic 5
Building, tagging, pushing, and pulling an image
This chapter assumes you can build an image from a Dockerfile, run the AWS CLI with working credentials, and that you have already created a private repository and logged Docker in to your registry. The examples use account 123456789012, Region us-east-1, and a repository named my-app. Replace those values with your own.
The workflow in one view
Pushing to ECR is the same as pushing to any other registry. The one difference is that the image name must contain the registry address, so Docker knows where to send it.
How an ECR image name is built
A full image reference has four parts:
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0
|-------------------------------------------| |----| |---|
registry host repo tag- The registry host is
<account-id>.dkr.ecr.<region>.amazonaws.com. It is fixed by your account and Region. - The repository name follows the first slash. It can contain further slashes, such as
team/my-app. - The tag follows the colon. If you omit it, Docker uses
latest.
Docker decides where to push by reading the host part of the name. An image named only my-app:1.0.0 would be sent to Docker Hub, and the push would fail or go to the wrong place. This is why tagging comes before pushing.
Build and tag
Build the image locally first.
docker build -t my-app:1.0.0 .Then add a second name that points to the same image. Tagging does not copy data. It only adds a new name to the same image ID.
docker tag my-app:1.0.0 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0You can combine both steps by building with the full name directly:
docker build -t 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0 .Check the result with docker images. You should see one image ID listed under both names.
Authenticate
Docker needs a token before it can push. ECR issues a temporary one through the AWS CLI.
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.comThree details matter here:
- The username is always the literal string
AWS. The password is the token. - The token is valid for 12 hours. A push that works in the morning can fail with an authorization error in the evening, and the fix is to log in again.
- A token is tied to one registry. If you work with two Regions or two accounts, log in to each registry host separately.
Push
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0The output lists each layer with a status. Layers that the repository already holds show Layer already exists and are skipped. This is why a second push of a slightly changed image is fast: only the changed layers travel over the network.
When the push finishes, Docker prints the image digest:
1.0.0: digest: sha256:4f1c... size: 1573Keep the digest. The next chapter section on tags and digests explains why it is the more reliable way to refer to an image.
What the push needs permission for
A push calls several ECR actions. Your IAM identity needs ecr:GetAuthorizationToken (on all resources) and, on the repository, these actions:
ecr:BatchCheckLayerAvailabilityecr:InitiateLayerUploadecr:UploadLayerPartecr:CompleteLayerUploadecr:PutImage
If a login succeeds but the push returns denied, one of the repository actions is usually missing.
Confirm what is stored
List the tags and digests in the repository:
aws ecr describe-images \
--repository-name my-app \
--region us-east-1 \
--query 'imageDetails[].{tags:imageTags,digest:imageDigest,pushed:imagePushedAt}'The result is a JSON list. Each entry is one image, identified by its digest, with zero or more tags attached. An image with no tags appears with tags set to null.
Pull
Pull by tag:
docker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0Or pull by digest, which always returns the same content:
docker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app@sha256:<digest>Replace <digest> with a value from the listing above. Note the @ in place of the :. A pull needs ecr:GetAuthorizationToken, plus ecr:BatchGetImage and ecr:GetDownloadUrlForLayer on the repository.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `no basic auth credentials` | Docker is not logged in to this registry, or the 12-hour token expired | Run the `get-login-password` command again |
| `repository ... does not exist` | The repository was not created, or the Region or account in the name is wrong | Create it with `aws ecr create-repository`, and check the host part of the name |
| `denied: User ... is not authorized to perform: ecr:...` | The IAM identity lacks a push or pull action | Add the named action to the identity policy or repository policy |
| `tag invalid: The image tag '1.0.0' already exists ... cannot be overwritten` | The repository has tag immutability on and the tag is taken | Push a new tag, such as `1.0.1` |
| Push goes to Docker Hub or fails with an `access denied` for `docker.io` | The image was not tagged with the registry host | Run `docker tag` with the full URI |
| `exec format error` when running a pulled image | The image was built for a different CPU architecture, for example on an Apple silicon laptop and run on x86 | Build with `docker buildx build --platform linux/amd64` for the target |
The architecture error appears at run time, not at push time. ECR stores whatever you give it, so a mismatched image is pushed and pulled without complaint.
Re-pushing a tag
If the repository allows mutable tags, pushing 1.0.0 again moves the tag to the new image. The old image stays in the repository but loses that tag, and it shows as untagged in describe-images. Any host that pulls 1.0.0 afterward gets different content than before. Immutability, covered in a later chapter, closes this gap. Until then, treat each version tag as write-once by habit.
Pushing several tags for one image
You can add more than one tag to the same image. Tag it again and push each name:
docker tag my-app:1.0.0 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:latest
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:latestThe second push uploads no layers. It only registers the new tag against the existing manifest. A moving tag such as latest is convenient for development, but production deployments should refer to a version tag or a digest.
Exercise
You build an image on your laptop with docker build -t my-app:2.0.0 ., then run docker push my-app:2.0.0. Docker reports that access is denied, even though you logged in to ECR successfully. What went wrong, and what two commands (or one build command) fix it?
Show answer
The image name has no ECR registry host, so Docker tried to push to Docker Hub, where your ECR login does not apply. Add the full repository URI as a tag with docker tag my-app:2.0.0 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:2.0.0, then run docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:2.0.0. Alternatively, build with the full URI as the tag from the start.
Exercise
A deployment pulls my-app:1.0.0 on Monday and again on Friday, and the two containers behave differently. The repository uses mutable tags. Give the likely cause and how to guarantee the same content on both days.
Show answer
Someone pushed a different image under the tag 1.0.0 in between, so the tag moved to new content. To guarantee identical content, reference the image by digest (my-app@sha256:<digest>), which cannot change, and enable tag immutability on the repository so version tags cannot be overwritten.