Sub topic 9
Combining the schemes in one release flow
This chapter assumes you can build and push images, you know how docker tag adds a name to an existing image, and you understand MAJOR.MINOR.PATCH versions. It also assumes you have seen why tags are mutable pointers. Here you will put the version, rolling, SHA, and latest tags together in one release.
Why use more than one scheme
Each scheme answers a different question.
| Tag | Example | Question it answers | Moves? |
|---|---|---|---|
| Commit SHA | `sha-3f9a1c2` | Which source commit is this? | No |
| Full version | `1.4.2` | Which release is this? | Should not |
| Minor | `1.4` | What is the newest patch of 1.4? | Yes |
| Major | `1` | What is the newest release of 1.x? | Yes |
| `latest` | `latest` | What was pushed most recently as a release? | Yes |
No single tag does all of this. A SHA tag is precise but hard to read. A version tag is readable but depends on you never reusing it. Rolling tags are convenient but change under the consumer. Together they give you traceability and convenience.
Build once, tag many times
The central rule is to build the image once and add every other tag to that same image. If you rebuild for each tag, the results can differ, because base images and package indexes change between builds. Then 1.4.2 and latest would not hold identical content.
Start with the SHA tag, because it is the most specific name for the build. Then point the other tags at it.
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 rolling tags from the version:
${VERSION%.*}removes the shortest suffix that matches.*, so1.4.2becomes1.4.${VERSION%%.*}removes the longest suffix that matches.*, so1.4.2becomes1.
After the push, five tags in the registry point at one image.
What happens on the next release
Suppose you release 1.4.3 from a new commit. The new build gets its own SHA tag and its own full version tag. The tags 1.4, 1, and latest move to the new image. The old 1.4.2 and its SHA tag stay where they were, so they remain available for rollbacks.
Decide who uses which tag
The scheme only helps if people consume the right tag.
- Production deployments should use the full version or the SHA. Both identify one release. For stricter guarantees, pin by digest, which a later chapter covers.
- Staging or test environments can follow a rolling tag such as
1.4to pick up patches automatically. - Local experiments can use
latest, since reproducibility matters less there. - Debugging starts from the SHA. Given a running container's tag,
git show 3f9a1c2shows the exact source.
Never move the full version tag after publishing it. If 1.4.2 is broken, release 1.4.3. Rolling tags exist to move, and the full version tag exists to stay put.
Pre-releases
Do not move latest or the rolling tags for a release candidate such as 1.5.0-rc.1. Give it only its SHA tag and its full pre-release tag. Add latest and the rolling tags when the stable 1.5.0 ships. This keeps consumers of latest on stable releases.
Exercise
You build commit 8b2d4e1 and release version 2.0.0. Which tags do you apply, and which of them will point at a different image after you release 2.0.1?
Show answer
Apply sha-8b2d4e1, 2.0.0, 2.0, 2, and latest, all to the one image built once. After 2.0.1, the tags 2.0, 2, and latest point at the new image. The tags sha-8b2d4e1 and 2.0.0 still point at the original image.
Exercise
Why does the release script tag the SHA first and derive the other tags from it, instead of running docker build four times with different -t values?
Show answer
Running separate builds can produce different content, for example if a base image or package changes between runs. Building once and adding tags with docker tag guarantees that every tag points at identical content.