Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readDocker
#docker#image-tags#ci#registries
Bold white headline reading BAD REFERENCE with a Docker tag crate rejected at an amber name gate on the right.

Introduction & Quick Summary

Docker refuses to parse an image name:

code
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 myimage where a shell variable expanded to nothing: docker tag app "$TAG" with TAG unset becomes docker tag app "".
  • A tag built with a commit SHA truncated to an empty string, or containing / from a branch name such as feature/x.
  • docker pull localhost:5000/app written as localhost:5000/:app or with a trailing colon.
  • docker run -v $(pwd):/app -p 8080:80 image where 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:

code
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 ":" hex

Three rules cause nearly every report:

  1. Repository path components are lowercase only; tags may be mixed case.
  2. A path component cannot start or end with a separator (_, ., -), and separators cannot repeat.
  3. 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:

bash
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:

bash
#!/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:

PartExampleAllowed charactersCase
registry hostghcr.iohostname, optional portlowercase
namespaceacme/webpath component grammarlowercase
repositoryapi-servicea-z0-9, separators . _ - betweenlowercase
tag1.2.3-rc1, sha-abc123alnum, . _ - after the first charmixed allowed
digestsha256:9f86d08...algorithm and hexlowercase hex

Key Comparison Table

The malformed strings people actually pass, and the corrected form:

InputWhy it failsCorrect
-t MyApp:latestuppercase 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 commitsempty tagdefault to latest when empty
-t app:feature/loginslash inside the tag-t app:feature-login or a sanitized branch name
-t ghcr.io/acme/App:1uppercase path component-t ghcr.io/acme/app:1
-t registry:5000/appregistry has no . or : so it is read as a namespace, not a host-t registry.local:5000/app
-t app:.1tag cannot start with .-t app:0.1
-t app:-rc1tag cannot start with --t app:rc1

Which Docker commands raise it, and what the real problem usually is:

CommandUsual causeCheck
docker build -tuppercase or empty tagecho the variable
docker tagunset variable, slash in tagset -u
docker runoption value missing, image name consumed as a flag valuequote every argument
docker pullport and colon confusionhost:port/path order
docker save / loadsame grammar applies to the archive name argumentquote 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 "":

bash
# 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:

bash
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:

bash
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 tag

The 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:

yaml
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 documentation

The Docker documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Docker guides

More in Docker

All Docker guides

The guides filed next to this one in Docker, in library order.