Docker: invalid reference format when tagging
Docker validates image names before the daemon runs. Lowercase the repository, reject empty variables and slashes in tags, and name registry hosts by port.

Introduction & Quick Summary
Docker refuses to parse an image name:
docker: invalid reference format.
See 'docker run --help'.or, when the repository part is present but not lowercase:
docker: invalid reference format: repository name must be lowercase.
The CLI failed before contacting the daemon. A "reference" is the full image string — registry:port/namespace/name:tag@digest — and Docker validates it against the distribution reference grammar. One character outside that grammar aborts the command, so the error is always about the string you passed, never about the image existing.
Where it appears:
docker build -t MyApp:latest .with an uppercase repository name.docker run -d myimagewhere a shell variable expanded to nothing:docker tag app "$TAG"withTAGunset becomesdocker tag app "".- A tag built with a commit SHA truncated to an empty string, or containing
/from a branch name such asfeature/x. docker pull localhost:5000/appwritten aslocalhost:5000/:appor with a trailing colon.docker run -v $(pwd):/app -p 8080:80 imagewhere a missing flag value shifts the argument parsing.
Quick answer: lowercase the repository, make sure every -t, --name, and image argument is non-empty, and validate the tag against ^[A-Za-z0-9_][A-Za-z0-9._-]{0,127}$ before the build runs.
Core Mechanics, Signatures & Reproducible Setup
The reference grammar is small and strict:
reference := name [":" tag] ["@" digest]
name := [domain "/"] path-component ["/" path-component]*
domain := host [":" port]
path := [a-z0-9]+ ([._-] [a-z0-9]+)*
tag := [A-Za-z0-9_][A-Za-z0-9._-]{0,127}
digest := algorithm ":" hexThree rules cause nearly every report:
- Repository path components are lowercase only; tags may be mixed case.
- A path component cannot start or end with a separator (
_,.,-), and separators cannot repeat. - Empty strings are never valid, so an unset variable that disappears leaves either an empty argument or a bare
:.
Reproduce each failure without pulling anything:
docker build -t MyApp:latest . # repository name must be lowercase
docker tag alpine "$TAG" # invalid reference format (TAG unset)
docker pull registry.local:5000/:app # invalid reference format
docker tag alpine app: # invalid reference format
docker tag alpine app:feature/x # invalid reference format (slash in tag)A quick lint in the shell catches all of them before Docker is invoked:
#!/usr/bin/env bash
set -euo pipefail
NAME="${1:?repository required}"
TAG="${2:-latest}"
if [[ ! "$NAME" =~ ^[a-z0-9]+([._-][a-z0-9]+)*$ ]]; then
echo "bad repository: $NAME" >&2; exit 1
fi
if [[ ! "$TAG" =~ ^[A-Za-z0-9_][A-Za-z0-9._-]{0,127}$ ]]; then
echo "bad tag: $TAG" >&2; exit 1
fi
docker build -t "$NAME:$TAG" .The parts of a reference and which rules apply to each:
| Part | Example | Allowed characters | Case |
|---|---|---|---|
| registry host | ghcr.io | hostname, optional port | lowercase |
| namespace | acme/web | path component grammar | lowercase |
| repository | api-service | a-z0-9, separators . _ - between | lowercase |
| tag | 1.2.3-rc1, sha-abc123 | alnum, . _ - after the first char | mixed allowed |
| digest | sha256:9f86d08... | algorithm and hex | lowercase hex |
Key Comparison Table
The malformed strings people actually pass, and the corrected form:
| Input | Why it fails | Correct |
|---|---|---|
-t MyApp:latest | uppercase repository | -t myapp:latest |
-t app: | empty tag after the colon | -t app:latest |
-t app:$(git rev-parse --short HEAD) in a repo with no commits | empty tag | default to latest when empty |
-t app:feature/login | slash inside the tag | -t app:feature-login or a sanitized branch name |
-t ghcr.io/acme/App:1 | uppercase path component | -t ghcr.io/acme/app:1 |
-t registry:5000/app | registry has no . or : so it is read as a namespace, not a host | -t registry.local:5000/app |
-t app:.1 | tag cannot start with . | -t app:0.1 |
-t app:-rc1 | tag cannot start with - | -t app:rc1 |
Which Docker commands raise it, and what the real problem usually is:
| Command | Usual cause | Check |
|---|---|---|
docker build -t | uppercase or empty tag | echo the variable |
docker tag | unset variable, slash in tag | set -u |
docker run | option value missing, image name consumed as a flag value | quote every argument |
docker pull | port and colon confusion | host:port/path order |
docker save / load | same grammar applies to the archive name argument | quote paths |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Empty variables, the most common cause in CI
set -e does not catch an empty variable; set -u does. Without it, this pipeline builds and tags "":
# broken: no failure when CI_COMMIT_TAG is empty on a branch build
docker build -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG" .
# fixed: explicit default plus a guard
: "${CI_REGISTRY_IMAGE:?registry image required}"
TAG="${CI_COMMIT_TAG:-$(git rev-parse --short HEAD)}"
docker build -t "${CI_REGISTRY_IMAGE}:${TAG}" .Echo the final string on one line before running Docker. In a pipeline the expanded command is what matters, and a log line Building myapp: is instantly recognisable.
2. Branch names are not tags
feature/login and release/2.4 contain a slash, which is legal in a branch and illegal in a tag. Sanitize deterministically so two branches never collapse into the same image:
RAW_BRANCH="${GITHUB_REF_NAME:-main}"
SAFE_TAG="$(printf '%s' "$RAW_BRANCH" | tr '/' '-' | tr '[:upper:]' '[:lower:]' | cut -c1-120)"
docker build -t "ghcr.io/acme/api:${SAFE_TAG}" .The cut keeps the tag inside 128 characters; a long branch name truncated silently by Docker is worse than a rejected one.
3. Registry host detection and ports
Docker decides whether the first path segment is a registry by looking for ., : or the literal localhost. registry:5000/app works because of the colon, while registry/app is treated as Docker Hub namespace registry. A misread host produces invalid reference format on the port when the colon is placed after the path:
docker pull registry.local:5000/acme/api:1.0 # correct
docker pull registry.local/acme/api:5000:1.0 # wrong: a second colon in the tagThe second form fails because the tag 5000:1.0 contains a colon. Move the port next to the host.
4. Compose files and image: values
The same grammar applies to image: in Compose, and an interpolated variable can empty it:
services:
api:
image: ${REGISTRY}/api:${TAG:-latest}If REGISTRY is unset, the value becomes /api:latest and Compose fails with an invalid reference rather than a missing variable. Give the variable a default or fail early:
image: ${REGISTRY:?set REGISTRY in .env}/api:${TAG:-latest}Compose supports the :? form for exactly this purpose. Validate the rendered file with the Compose formatter before starting the stack.
5. When the argument parser, not the name, is wrong
A missing value for a flag makes the next token the value. In docker run -v $(pwd):/app -it myimage, an empty $(pwd) produces -v :/app, which Docker reports as an invalid volume specification; but docker run --name -d myimage consumes -d as the name and then reports the image myimage as an invalid reference if it is not a valid name. Quote arguments and put image names last:
docker run --rm -it \
-v "${PWD}:/app" \
-p 8080:80 \
"${IMAGE:?image required}"When a tag must be derived from a computed value, test the finished string with the regular expression tester using the grammar above; it is faster than waiting for a build to fail.
Common Compilation/Runtime Mistakes & Version Gotchas
Lowercasing the whole reference. docker build -t $(echo "$NAME" | tr 'A-Z' 'a-z') also lowercases a digest or tag that was valid, and silently changes which image you push. Lowercase only the repository parts.
Retagging to fix it in place. docker tag APP app:latest fails with the same message because both sides are parsed. The first argument must already exist as a valid reference; rename the local image build instead.
Assuming Docker Hub accepts the same slugs as GitHub. GitHub repository names allow uppercase and dots, Docker Hub repositories do not allow uppercase. A working local build can still fail at docker push for the same grammar reason, so apply the sanitizer to the name, not only to the tag.
Version differences worth knowing. Docker 23 and later use containerd for image storage by default, and the client validates references before the daemon sees them, so the error appears even when the daemon is not running. Buildx validates the same way but reports invalid tag format in some versions for the tag-only case, which confuses searches for the exact string. Podman accepts a superset in some 4.x releases and then fails on push, which is why a script that works locally can fail in CI.
Checking the tag that actually exists. After a successful push, docker manifest inspect and docker buildx imagetools inspect show tags and digests for a registry without pulling. Confirm the exact string you pushed with the hash utility when comparing a commit SHA with the tag in the registry.
Reference material: docker image tag and docker build.
Official references
primary documentationThe Docker documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Docker guides
All Docker guidesDocker build: failed to compute cache key: not found
BuildKit cannot hash a file that is not in the context. Correct the context path, the .dockerignore rules, or the COPY source and stage name.
Docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock
An endpoint mismatch, a stopped daemon, or a missing socket mount. Resolve which of the three, then start, retarget, or mount.
Docker Compose: bind: address already in use and Port Conflicts
A published port is a host socket, and two projects cannot own it. How to find the real owner, why an override file merged instead of replacing, and how.
Docker Buildx: Building Multi-Architecture Images (AMD64 & ARM64) Explained
One tag, several architectures: the driver that supports it, QEMU versus cross-compilation, registry cache, and reading the manifest list.
More in Docker
All Docker guidesThe guides filed next to this one in Docker, in library order.