Sub topic 6
Tagging releases with semantic versions
This chapter assumes you can build and push Docker images, know that a tag is a movable label on an image (covered earlier in this tutorial), and understand Git tags and the MAJOR.MINOR.PATCH format of semantic versioning.
Why semantic versions for images
A semantic version (semver) such as 1.4.2 tells consumers what kind of change a release contains. Using it as an image tag gives your users the same signal:
- MAJOR changes break compatibility, for example
1.4.2to2.0.0. - MINOR changes add features without breaking anything, for example
1.4.2to1.5.0. - PATCH changes fix bugs, for example
1.4.2to1.4.3.
A tag like 1.4.2 is meant to name one release forever. Your team and your users can then say "we run 1.4.2" and everyone knows what that means.
Tagging a release
Build once, then add the other tags to the same image. Every tag is a label on identical content, so nothing is rebuilt.
IMAGE=registry.example.com/team/myapp
docker build -t $IMAGE:1.4.2 .
docker tag $IMAGE:1.4.2 $IMAGE:1.4
docker tag $IMAGE:1.4.2 $IMAGE:1
docker tag $IMAGE:1.4.2 $IMAGE:latest
docker push --all-tags $IMAGEThe full tag 1.4.2 is the one you never move. The shorter tags 1.4 and 1 are rolling tags, which the next chapter covers. This chapter focuses on the full version tag.
Deriving tags from one version string
Typing each tag by hand invites mistakes. In a release script, keep the version in one variable and derive the shorter forms with shell parameter expansion.
VERSION=1.4.2
echo "${VERSION%.*}" # 1.4 (removes the shortest suffix matching .*)
echo "${VERSION%%.*}" # 1 (removes the longest suffix matching .*)This works for plain MAJOR.MINOR.PATCH strings. It does not handle pre-release versions, as shown below.
Pre-releases and build metadata
Semver allows a pre-release suffix after a hyphen, such as 2.0.0-rc.1, and build metadata after a plus sign, such as 2.0.0+build.5. Docker tags can contain letters, digits, underscores, periods, and hyphens. They cannot contain +. The usual convention is to replace + with _ or - when you tag.
| Semver string | Valid Docker tag? | Tag to use |
|---|---|---|
| `1.4.2` | Yes | `1.4.2` |
| `2.0.0-rc.1` | Yes | `2.0.0-rc.1` |
| `2.0.0+build.5` | No, contains `+` | `2.0.0_build.5` |
Do not apply the %.* trick to 2.0.0-rc.1. It would produce 2.0.0-rc, which looks like a minor tag but is not one. Pre-releases normally get only their full tag, so that 2 and latest never point at a release candidate.
Keep the Git tag and the image tag in step
A common practice is to make the Git tag the source of truth. You create v1.4.2 in Git, and the pipeline strips the v and builds 1.4.2. That way the image tag, the Git tag, and the release notes all agree.
GIT_TAG=v1.4.2
VERSION=${GIT_TAG#v}
echo "$VERSION" # 1.4.2Common errors
- Re-pushing an existing version. If you push
1.4.2again with different content, anyone who pulls it later gets something different from earlier users. Fix the bug and release1.4.3instead. - Including
+in a tag. Docker rejects it as an invalid reference format. Replace it first. - Mixing
v1.4.2and1.4.2. Pick one form for images and stay with it. Most registries and tools treat these as unrelated tags. - Tagging a release candidate as
latest. Only stable releases should movelatest.
Exercise
Your Git tag is v3.1.0-beta.2. What Docker tag should the image receive, and should the tags 3.1, 3, and latest also be moved to it?
Show answer
Strip the v to get 3.1.0-beta.2, which is a valid Docker tag. Do not move 3.1, 3, or latest, because a pre-release should only receive its own full tag so that stable consumers never receive it.