Sub topic 8
Tagging with the commit SHA
This chapter assumes you can build and push images, read git log, and know that a Docker tag is a movable label on image content. Semantic version tags tell users which release they have. A commit SHA tag tells you which source code produced a given image.
Why tag with a commit SHA
A version tag such as 1.4.2 is a human decision. Someone can forget to bump it, or rebuild from a different commit and push it again. A commit SHA is the hash Git assigns to a commit. If you tag an image with that hash, you can trace a running container back to the exact source.
The SHA tag also has a practical advantage. Each commit has a different SHA, so each build gets a new tag. You do not overwrite an earlier image by accident, and every build stays addressable.
Building and pushing a SHA tag
Ask Git for a short SHA with git rev-parse. The --short=7 option fixes the length at seven characters, so the tags stay uniform.
IMAGE=registry.example.com/team/myapp
SHA=$(git rev-parse --short=7 HEAD)
docker build -t $IMAGE:sha-$SHA .
docker push $IMAGE:sha-$SHAThe sha- prefix is a convention. A tag made only of hex digits, such as 3f9a1c2, is easy to confuse with an image ID or a number. The prefix makes the tag's purpose obvious. It also keeps the tag valid when a SHA happens to start with a digit, because tags are easier to read and filter when they share a pattern.
flowchart LRHow the SHA tag fits with other tags
The SHA tag is usually the first tag you apply. You build once with it, then add the other tags to the same image. All the tags then point at identical content.
IMAGE=registry.example.com/team/myapp
VERSION=1.4.2
SHA=$(git rev-parse --short=7 HEAD)
docker build -t $IMAGE:sha-$SHA .
docker tag $IMAGE:sha-$SHA $IMAGE:$VERSION
docker tag $IMAGE:sha-$SHA $IMAGE:${VERSION%.*}
docker tag $IMAGE:sha-$SHA $IMAGE:${VERSION%%.*}
docker tag $IMAGE:sha-$SHA $IMAGE:latest
docker push --all-tags $IMAGEThe shell expansions derive the other tags from VERSION. ${VERSION%.*} removes the last dot and everything after it, which gives 1.4. ${VERSION%%.*} removes the first dot and everything after it, which gives 1.
Tracing a container back to source
Suppose a container misbehaves in production and its image is tagged sha-3f9a1c2. You can inspect the commit directly:
git show 3f9a1c2You see the author, the date, and the diff. You can also check out that commit and rebuild locally to reproduce the problem. This only works if the image was built from a clean working tree. If you build with uncommitted changes, the SHA does not describe what is inside the image.
Common errors
- Too short a SHA. Very short hashes can become ambiguous in large repositories. Seven characters is a common default, and Git lengthens it automatically when needed. Use the same length everywhere you tag and look up images.
- Tagging before committing.
HEADpoints at the last commit, so edits you have not committed are built but not recorded in the tag. - Treating the SHA tag as a digest. A SHA tag is still a tag. The Git commit is fixed, but someone with push access can re-push the tag to different content. For exact content pinning, use the image digest, covered in a later chapter.
- Different SHA length in different tools. If your CI uses 8 characters and your script uses 7, the tags will not match. Pick one length and keep it.
Exercises
Exercise
You run docker build -t $IMAGE:sha-$SHA . and then docker tag $IMAGE:sha-$SHA $IMAGE:1.4.2. How many images exist, and why is that useful?
Show answer
One image with two tags. Both tags point at identical content, so the version tag and the SHA tag always agree, and you can trace the released version to its commit.
Exercise
A teammate builds an image while they have uncommitted edits, tagging it with the output of git rev-parse --short=7 HEAD. What is the problem?
Show answer
The tag names the last commit, but the image also contains uncommitted changes. Checking out that commit will not reproduce the image, so the tag misleads anyone tracing the source.