Sub topic 12
Tag Immutability and Retention in Registries
This chapter assumes you can build and push images, know the tag and digest parts of an image reference, and have read the earlier chapters on semantic version tags, rolling tags, and commit SHA tags. Here you will see how a registry can enforce the tagging rules you chose, and how to stop old images from piling up.
Why the registry should enforce the rules
A tag is a movable label. Your team may agree never to re-push 1.4.2, but a script with a typo, a re-run CI job, or a rushed hotfix can still overwrite it. A registry policy turns the agreement into a hard rule.
Two kinds of policy matter:
- Tag immutability decides whether an existing tag can be pointed at different content.
- Retention (also called lifecycle) decides which images are deleted, and when.
The two interact. A rolling tag such as 1.4 or latest must move on every release, so it cannot be immutable. A tag such as 1.4.2 or sha-3f9a1c2 should never move.
Tag immutability
With immutability on, a push to an existing tag fails if the content differs. The details vary by registry:
| Registry | Immutability support |
|---|---|
| Amazon ECR | Repository-level setting, either mutable or immutable. Newer versions can exclude chosen tag patterns, such as `latest`, from the rule. |
| Docker Hub | Repository-level option on paid plans. Check your plan's current features. |
| Harbor | Tag immutability rules per project, matched by repository and tag patterns. |
| GitHub Container Registry | No tag immutability setting. Use digests and process controls instead. |
Check your registry's current documentation, since these features change.
The conflict with rolling tags
If a whole repository is immutable on a registry that cannot exclude patterns, the first push of latest, 1, or 1.4 is final. The next release cannot move it. You have three options:
- Use a registry that supports pattern-based rules. Make
*.*.*andsha-*immutable, and leavelatest,1, and1.4mutable. - Split into two repositories: one immutable repository for release tags, one mutable repository for rolling tags.
- Stop publishing rolling tags and have consumers pin full versions or digests.
Option 1 is usually the best fit.
Seeing immutability fail
This is what a rejected push looks like on an immutable ECR repository. The exact message depends on the registry.
IMAGE=123456789012.dkr.ecr.eu-west-1.amazonaws.com/team/myapp
docker tag myapp:rebuilt $IMAGE:1.4.2
docker push $IMAGE:1.4.2
# Fails with an error such as:
# tag invalid: The image tag '1.4.2' already exists in the
# 'team/myapp' repository and cannot be overwritten because
# the repository is immutableIn CI, treat this failure as a signal. It usually means a job ran twice, or someone released from a branch without bumping the version.
Retention policies
Every build pushed with a SHA tag adds an image. Without cleanup, storage costs grow and repositories fill with images nobody will deploy. Retention rules delete images by age, count, or tag pattern.
A sensible policy treats tag types differently:
| Tag pattern | Suggested retention | Reason |
|---|---|---|
| `1.4.2` (full versions) | Keep indefinitely, or a long window | You may need to roll back or reproduce a release. |
| `1`, `1.4`, `latest` | Never expire | These tags always point at a current image. |
| `sha-*` | Keep the most recent N images, or 30 to 90 days | Useful for tracing recent builds. Old ones rarely matter. |
| Untagged images | Delete after a few days | These are left behind when a tag moves. |
Untagged images and moving tags
When you push a new image to latest, the previous image loses that tag. If it has no other tag, it becomes untagged and is a candidate for cleanup. This is why every image you release should also carry a version or SHA tag. An image reachable only through a moving tag can vanish from the registry after the tag moves.
Here is an ECR lifecycle policy that keeps the 20 newest sha- images and removes untagged images after 7 days:
{
"rules": [
{
"rulePriority": 1,
"description": "Expire untagged images after 7 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 7
},
"action": { "type": "expire" }
},
{
"rulePriority": 2,
"description": "Keep only the 20 newest sha- images",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["sha-"],
"countType": "imageCountMoreThan",
"countNumber": 20
},
"action": { "type": "expire" }
}
]
}Exercise
Exercise
Your registry repository is set to fully immutable. Your release script pushes 1.4.2 and then moves 1.4 and latest to the same image. The first release works, but the next release fails when pushing latest. What is the cause, and what is one fix?
Show answer
Immutability blocks any change to an existing tag, including 1.4 and latest, which must move on each release. Fix it by using a registry that supports exclusion patterns, so latest and 1.4 stay mutable while *.*.* and sha-* stay immutable. Alternatively, move rolling tags into a separate mutable repository.