Sub topic 3
What a valid tag looks like
This chapter assumes you can build, tag, and push images with Docker, and that you know the semantic versioning format MAJOR.MINOR.PATCH. It covers which characters a tag may contain and how to fit semantic version strings into those limits.
The rules
A tag is the part of an image reference after the last colon, as in myapp:1.4.2. Docker and the OCI distribution spec accept a tag only if it follows these rules:
- It contains only letters (
A-Z,a-z), digits, underscores, periods, and dashes. - It is at most 128 characters long.
- It does not start with a period or a dash.
As a regular expression, that is [A-Za-z0-9_][A-Za-z0-9_.-]{0,127}.
Tags are case-sensitive, so Release and release are different tags. Repository names are stricter than tags. They must be lowercase, which is why you can have myapp:RC1 but not MyApp:rc1.
| Tag | Valid | Reason |
|---|---|---|
| `1.4.2` | Yes | Digits and periods |
| `1.5.0-rc.1` | Yes | A dash and a period are allowed inside a tag |
| `sha-3f9a1c2` | Yes | Letters, a dash, digits |
| `-beta` | No | Starts with a dash |
| `.hidden` | No | Starts with a period |
| `1.5.0+build.7` | No | `+` is not an allowed character |
| `release/1.4` | No | `/` separates repository path segments, not tag text |
| `my tag` | No | Spaces are not allowed |
Where the colon belongs
Docker reads the reference from the right. A colon followed by a slash belongs to a registry port, and the last colon with no slash after it starts the tag. In registry.example.com:5000/team/myapp:1.4.2, the first colon is the port and the second starts the tag. An @ introduces a digest instead, as in myapp@sha256:.... Digests are covered in a later chapter.
Fitting semantic versions into tags
A semantic version can have two extras after the core number:
- A pre-release part after a dash, such as
1.5.0-rc.1. This is already a legal tag, so you can use it unchanged. - Build metadata after a plus sign, such as
1.5.0+build.7. The+is not legal in a tag.
Docker rejects the + form at tag time:
docker tag myapp:latest myapp:1.5.0+build.7This fails with an invalid reference format error. The usual fix is to replace + with _, giving 1.5.0_build.7. Helm does the same when it publishes charts to OCI registries, so tools in this ecosystem already expect it. Keep in mind that semantic versioning ignores build metadata when comparing versions. Two tags that differ only after the underscore are still different tags to a registry, and they can point at different images.
The script below converts a version string and checks the result against the tag rules before you use it:
#!/usr/bin/env bash
set -euo pipefail
VERSION="${1:-1.5.0-rc.1+build.7}"
TAG="${VERSION//+/_}"
if [[ ! "$TAG" =~ ^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$ ]]; then
echo "invalid tag: $TAG" >&2
exit 1
fi
echo "$TAG"With the default input it prints 1.5.0-rc.1_build.7. Passing -beta makes it exit with invalid tag: -beta.
Common errors
- A leading
v. Git tags are oftenv1.4.2, andv1.4.2is a valid Docker tag. Pick one style, with or without thev, and use it everywhere. Otherwisev1.4.2and1.4.2become two unrelated tags. - Branch names. A branch such as
feature/logincontains a slash. Replace it first, for examplefeature-login. - Empty variables. If a shell variable is empty,
myapp:$VERSIONbecomesmyapp:. Docker rejects this withinvalid reference format. Usingset -u, as in the script above, makes the shell stop on an unset variable.
Exercise
Your Git release tag is v2.0.0-beta.2+exp.sha.5114f85. What Docker tag would you produce, and why can't you use the string unchanged?
Show answer
Replace the + with _, giving v2.0.0-beta.2_exp.sha.5114f85 (or 2.0.0-beta.2_exp.sha.5114f85 if you drop the v by convention). The + is not an allowed tag character, so Docker would reject the original string with invalid reference format. Everything else (letters, digits, periods, dashes) is allowed, and the result is well under 128 characters and does not start with a period or dash.