Sub topic 13
Common mistakes and how to roll back
This chapter assumes you know how to build and push images, how tags and digests fit into an image reference, and how the version, rolling, SHA, and latest tags work. It covers the mistakes that cause trouble in practice and how to recover from a bad release.
Mistakes that break traceability
Most tagging problems come from one fact: a tag is a movable pointer, and a registry will let you move it.
- Re-pushing a version tag. If you rebuild and push
1.4.2again after a fix, the old content is no longer reachable by that name. Hosts that already pulled1.4.2keep the old image, and new hosts get the new one. Publish1.4.3instead. - Deploying
latest. The name says nothing about which release it points to. Two nodes that pull at different times can run different code, and you cannot tell from the deployment file what is running. - Deploying a rolling tag and not recording what it resolved to.
1.4is convenient, but it moves with every patch release. If you do not note the digest or SHA, you cannot say afterwards which patch was live. - Tagging from a dirty working tree. A
sha-tag only traces to a commit if the build used exactly that commit. Build from a clean checkout, or the tag will point at code that is not in Git. - Invalid characters. Semantic versions allow
+for build metadata, such as1.4.2+build5, but Docker tags do not. Replace the+with-or_. - Pushing only some tags.
docker push $IMAGE:1.4.2uploads that one tag. If you added1.4andlatestlocally, they stay local until you push them or use--all-tags. - Retention rules that delete tags in use. A cleanup policy that removes old tags can delete the image you need for a rollback. Exempt every tag that is currently deployed or is the previous release.
Rolling back
The safest rollback is a redeploy of an earlier tag that was never moved. Because each sha- and full version tag refers to fixed content, you only change which name the deployment uses.
flowchart TDIf your services follow a rolling tag such as 1.4 or latest, move those tags back to the last good release and push them again:
IMAGE=registry.example.com/team/myapp
docker pull $IMAGE:1.4.1
docker tag $IMAGE:1.4.1 $IMAGE:1.4
docker tag $IMAGE:1.4.1 $IMAGE:latest
docker push $IMAGE:1.4
docker push $IMAGE:latestRunning containers do not change when you move a tag. Restart or redeploy them so they pull the tag again. Leave 1.4.2 in the registry so you can investigate the failure.
To verify the result, compare the digest of what is running with the digest of the tag you expect:
docker inspect --format '{{index .RepoDigests 0}}' registry.example.com/team/myapp:1.4.1A prevention checklist
- Deploy production workloads by version tag, SHA tag, or digest, never by
latest. - Treat full version tags as write-once. Turn on tag immutability in the registry if it supports it, and expect it to also block moving rolling tags, so check whether your registry can exempt them.
- Record the digest of every release in your deploy log.
- Keep at least the previous release's tags out of any cleanup rule.
Exercise
Version 1.5.0 is live and broken. Your services run myapp:1.5. The last good release was 1.4.7. What do you do, and why is deleting the 1.5.0 tag a poor choice?
Show answer
Pull myapp:1.4.7, tag it as myapp:1.5, push myapp:1.5, and restart the services so they pull the tag again. Deleting 1.5.0 is poor because it removes the evidence you need to debug, and hosts that cached the image would still run it under a name that no longer exists in the registry. Fix the bug and publish 1.5.1.