docker: invalid reference format with an image name that looks fine usually
means Docker never saw your image name. An unquoted path with a space in -v
split the command, or an empty variable left a name like alpine:. Quote the
argument or give the variable a value.
Docker reads the first word after the options as the image reference. When a stray piece
of a path or a half-built name lands in that position, it fails the reference grammar
before any pull or run happens. Put quotes around every -v argument that
contains a path, and give tag variables a default.
The error
docker: invalid reference format
Run 'docker run --help' for more information
The exit code was 125 in both shells.
Why it happens
A path with a space. In PowerShell,
-v ${PWD}\My Data:/data is two arguments: -v receives
...\My and the next word, Data:/data, becomes the image
reference. Here the colon makes Docker read /data as a tag, and a tag cannot
hold a slash, so Docker rejects it as a format error.
An empty variable. In a script that builds the name as
"$IMAGE:$TAG", an unset TAG leaves alpine:. A colon
followed by nothing is not a valid tag, so the same message appears. A capital letter in
the name gives a different, more helpful message; that case has
its own
post.
The fix
Quote the whole volume argument (PowerShell, the folder My Data holds one file):
PS> docker run --rm --name fix8-cli-a -v "${PWD}\My Data:/data" alpine ls /data
hello.txt
In Bash, set the tag or give the variable a default with ${VAR:-value}:
$ TAG=latest
$ docker run --rm --name fix8-cli-b "$IMAGE:$TAG" echo hi
hi
$ docker run --rm --name fix8-cli-b "$IMAGE:${TAG2:-latest}" echo hi
hi
When the message appears in a long script, print the exact command before running it
(echo in Bash, Write-Host in PowerShell) and look at the word
just before the container command. Whatever sits there is what Docker tried to parse as
the image.
How it was reproduced
Docker Desktop on Windows 11 with client and engine 29.9.2 and the cached
alpine:latest image. The space case ran in Windows PowerShell 5.1 from a
folder holding a My Data subfolder; the empty-variable case ran in Git Bash
5.2.26 with IMAGE=alpine and TAG never set. Both printed the
message above and exit code 125; the quoted and defaulted versions ran the container.
Frequently asked
- Why does docker run say invalid reference format when my image name is lowercase?
- Docker may not be reading your image name at all. An unquoted path with a space in -v pushes part of the path into the image position, and an empty tag variable leaves a trailing colon. Print the command and check the word before the container command.
- How do I mount a Windows folder with spaces in Docker?
- Put the whole -v argument in double quotes, for example -v "${PWD}\My Data:/data" in PowerShell. The host path, the colon and the container path must reach Docker as one argument.
- How do I give a Docker tag variable a default in Bash?
- Write "$IMAGE:${TAG:-latest}". When TAG is unset or empty, Bash puts latest in its place, so Docker never sees a name that ends in a colon.
More decoded errors in the Fixes category.