Sub topic 6
Tags versus digests
This chapter assumes you can build, tag, and push an image to ECR as covered in the previous chapter, and that your AWS CLI credentials are configured.
Two ways to name an image
Every image in a repository can be referenced in two ways.
- A tag is a human-readable label such as
1.0.0orlatest. It is a pointer, and it can be moved to a different image. - A digest is a SHA-256 hash of the image manifest, written as
sha256:followed by 64 hex characters. It is computed from the content, so it identifies exactly one image and never changes.
One image can carry several tags. A tag points to only one image at a time. Pushing a tag that already exists, in a repository where tags are mutable, moves that tag to the new image. The old image keeps its digest but loses the tag, so it becomes untagged.
Looking up tags and digests
Use describe-images to see how tags map to digests in your repository.
aws ecr describe-images \
--repository-name my-app \
--region us-east-1 \
--query 'imageDetails[].{tags:imageTags,digest:imageDigest,pushed:imagePushedAt}'An untagged image shows null for tags. To get the digest behind one tag, filter by tag and print only the digest:
aws ecr describe-images \
--repository-name my-app \
--image-ids imageTag=1.0.0 \
--region us-east-1 \
--query 'imageDetails[0].imageDigest' \
--output textPulling by digest
Replace <digest> with a value from the listing. The part after @ is the digest.
docker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app@sha256:<digest>This command returns the same bytes every time. If someone later pushes a different image under 1.0.0, the digest reference is not affected.
When to use which
| Situation | Reference to use | Reason |
|---|---|---|
| Local development, quick tests | Tag | Easy to read and type |
| Production deployments | Digest | Reproducible and cannot change underneath you |
| Rolling forward with new releases | New, unique tag | Gives each release a name people can talk about |
| Auditing what was deployed | Digest | Maps to exactly one image |
A common pattern is to tag each build with a version or commit ID, then record the digest in your deployment configuration. People read the tag, and the deployment uses the digest.
Common errors
- Deploying
latest. A rollout today and a rollout next week may run different code, becauselatestmoves. A node that cached an olderlatestcan also run different code from a node that pulled a newer one. - Assuming a tag is stable. In a mutable repository, anyone with push permission can move any tag. Tag immutability, covered in the next chapter, stops this.
- Losing the old image. After a tag moves, the previous image is untagged. A lifecycle policy that expires untagged images will eventually delete it, so a rollback to that image fails unless you referenced it some other way.
- Digest mismatch across registries. The digest depends on the manifest. If you push the same image to another registry in a way that changes the manifest, for example by converting its format, the digest can differ. Replication within ECR keeps digests, but copy tools may not.
Exercise
Your team deploys my-app:latest to production. A rollback to "the previous latest" fails because the image is gone. Give two causes related to tags and digests, and one change that prevents the problem.
Show answer
First, pushing a new image to latest moved the tag, which left the previous image untagged. Second, a lifecycle policy that expires untagged images then deleted it. To prevent this, tag every build with a unique version and deploy by digest. You can also turn on tag immutability so tags cannot be moved.