246

I'm following this tutorial that uses Docker. When I tried to run Docker (inside the run.sh script):

docker run \
    -p 8888:8888 
    -v `pwd`/../src:/src \
    -v `pwd`/../data:/data -w /src supervisely_anpr \
    --rm \
    -it \
    bash

I got the error:

docker: invalid reference format.

I spent 2 hours and I can't really understand what's wrong. Any idea really appreciated.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
ai2016
  • 2,721
  • 2
  • 11
  • 19
  • see if the following helps you https://stackoverflow.com/a/45281200/1398418 – Oleg Aug 14 '17 at 19:57
  • 6
    Try this `docker run -p 8888:8888 -v "\`pwd\`/../src":/src -v "\`pwd\`/../data":/data -w /src --rm -it supervisely_anpr bash` – Tarun Lalwani Aug 14 '17 at 21:08
  • 5
    Like @TarunLalwani and @Oleg mentioned you'll need to move the `--rm` and `-it` in-between `run` and the image name. That won't explain the error message, though. Did you check whether the image name characters don't have any special encoding or upper case? Copy&Paste from your snippet works for me, while `docker run --rm foo! bash` prints the same error like yours. – gesellix Aug 14 '17 at 21:28
  • 3
    considering posting an answer to this so is easier to find for the next guy – lloiacono Feb 15 '18 at 09:58
  • 9
    Always double-quote dollar expansions unless you really want to split the string into words. Here, use `"$(pwd)"` (modern form of `"\`pwd\`"`). Your command becomes `docker run -p 8888:8888 -v "$(pwd)"/../src:/src -v "$(pwd)"/../data:/data -w /src supervisely_anpr --rm -it bash`. – Martin Jambon Jun 21 '19 at 18:17
  • my issue was the docker-compose.yml pointed to http:// url. I removed the http:// and it worked – high_byte May 15 '23 at 20:42

27 Answers27

255

In powershell you should use ${pwd} instead of $(pwd)

SiddAjmera
  • 38,129
  • 5
  • 72
  • 110
Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
  • 31
    THIS is what caused me to bang my head into the wall (sometimes I hate M$FT) – beerwin Jan 15 '19 at 10:25
  • 1
    `docker run --rm -ti --name zalenium -p 4444:4444 -p 5555:5555 \ -e SAUCE_USERNAME -e SAUCE_ACCESS_KEY \ -v /tmp/videos:/home/seluser/videos \ -v /var/run/docker.sock:/var/run/docker.sock \ dosel/zalenium start --sauceLabsEnabled true` what's wrong with my command? It is also giving same error. – paul Feb 04 '19 at 05:45
  • ... and just `$pwd` does not work, which is THE obvious. – Timo Oct 15 '20 at 12:12
  • 1
    For anyone having problem like @paul for a perfectly correct command, Please refer to this answer https://stackoverflow.com/a/65690853/807104 – Mohd Abdul Mujib Jan 12 '21 at 19:37
  • 2
    Why does this work? `$(pwd)` and `${pwd}` seem to output the exact same thing when I run the two commands in Powershell. – Josh Aug 13 '21 at 15:51
  • 2
    The question isn't `powershell`-tagged, so changing `\`pwd\`` to `${pwd}` _alone_ wouldn't help, due to the ``\``-based line continuations. It's not clear where the `$(pwd)` expression you're advising against comes from, given that it isn't mentioned in the question. `$(pwd)` _does_ work in PowerShell (even though `$pwd` is preferable), _as long as you use it inside `"..."`_, which is generally advisable, e.g., `"$(pwd)/../src:/src"` (but `"$pwd/../src:/src"` is better). It's just that `$(pwd)`, as a _subexpression_, happens not work as the start of an _unquoted_ compound token /cc @Josh – mklement0 Oct 27 '22 at 02:14
68

The first argument after the "run" that is not a flag or parameter to a flag is parsed as an image name. When that parsing fails, it tells you the reference format, aka image name (but could be an image id, pinned image, or other syntax) is invalid. In your command:

 docker run -p 8888:8888 -v `pwd`/../src:/src -v `pwd`/../data:/data -w /src supervisely_anpr --rm -it bash

The image name "supervisely_anpr" is valid, so you need to look earlier in the command. In this case, the error is most likely from pwd outputting a path with a space in it. Everything after the space is no longer a parameter to -v and docker tries to parse it as the image name. The fix is to quote the volume parameters when you cannot guarantee it is free of spaces or other special characters.

When you do that, you'll encounter the next error, "executable not found". Everything after the image name is parsed as the command to run inside the container. In your case, it will try to run the command --rm -it bash which will almost certainly fail since --rm will no exist as a binary inside your image. You need to reorder the parameters to resolve that:

 docker run --rm -it -p 8888:8888 -v "`pwd`/../src:/src" -v "`pwd`/../data:/data" -w /src supervisely_anpr  bash

I've got some more details on these two errors and causes in my slides here: https://sudo-bmitch.github.io/presentations/dc2018/faq-stackoverflow-lightning.html#29

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 3
    *Slide 30* **solved** my problem. I had a .sh file running in Cygwin. I changed `# !/bin/bash`(notice the space between `#`and `!` to `#!/bin/bash` and then the `End of Line Sequence` from `CRLF` to `LF`. That was all i did. Thanks @BMitch for the sharing the slides. – blongho Jan 16 '19 at 00:07
  • 1
    `The image name "supervisely_anpr" is valid` solved it for me, having the same issue in another context. I simply had an invalid image name (it did not exist, I had to build it first with another sh script that i had overseen). During testing, I also removed the image name from the command. One of the two things caused the error. – questionto42 Dec 22 '21 at 19:15
68

I had the same issue when I copy-pasted the command. Instead, when I typed-in the entire command, it worked!

Good Luck...

Aakash
  • 21,375
  • 7
  • 100
  • 81
  • 5
    In my case it was the issue of -- and quotes converted to some fancy utf-8 characters that docker did not recognised. – Piotr Czapla May 19 '21 at 12:11
  • 3
    In my case it was an "en dash" instead of an ordinary hyphen. This can happen if you copy and paste the command from a web page, or, as in my case, from a PDF file that was intended to be used as a docker cheat sheet. – Cito Jan 29 '22 at 13:40
  • 1
    Thanks you, for me it was the " \ " caracter of the n8n documentation that I deleted – Alexy Mar 17 '22 at 08:41
  • 1
    ok, that solved my issue... – muammar Apr 06 '22 at 20:46
  • 1
    The what..? Haha easy to miss this, nice one! – Erick Boshoff Aug 29 '22 at 20:01
  • 1
    for me it's because I have extra space after backslash(\), need to remove the extra spaces because backslash is expecting a new line right after it. – Rosemary Jan 11 '23 at 18:24
16

I ran into this issue when I didn't have an environment variable set.

docker push ${repo}${image_name}:${tag}

repo and image_name were defined but tag wasn't.

This resulted in docker push repo/image_name:.

Which threw the docker: invalid reference format.

brandonbanks
  • 1,125
  • 1
  • 14
  • 21
  • I had the same problem and I fixed it by setting the missing environment variable export tag=latest – PHZ.fi-Pharazon Apr 01 '20 at 13:12
  • `docker build` failed for me with this error on `FROM ${ORG}/ci-common:${CI_COMMON_VERSION} as builder`. It turned out to be caused by a simple typo in the preceding `ARG CI_COMMON_VERION=2` (missing an `S`), which I had copied from another script which had this typo everywhere and was thus working fine. – CodeManX Sep 18 '20 at 09:33
  • I also had this error where I accidentally typed "example.org:foo/bar:latest" instead of "example.org/foo/bar:latest" – Adam Jan 25 '21 at 20:54
12

I had a similar problem. Issue I was having was $(pwd) has a space in there which was throwing docker run off.

Change the directory name to not have spaces in there, and it should work if this is the problem

Nilesh
  • 1,388
  • 6
  • 17
12

I was having the same problem on Linux. That was because of directory names contained spaces. I fixed it by using "$(pwd)/path/to/go" instead of $(pwd)/path/to/go

Yash Rathi
  • 551
  • 7
  • 9
8

For others come here:
If you happen to put your docker command in a file, say run.sh, check your line separator. In Linux, it should be LR, otherwise you would get the same error.

Sean
  • 1,055
  • 11
  • 10
  • This can happen if you use WSL on Windows. You could end up with mixed line endings if you do not pay enough attention. For example, when you create a new text file from context menu. – FonzTech Mar 16 '21 at 15:34
7

I was executing the whole command in one line, as it was mentioned as such

$ docker run --name testproject-agent \
-e TP_API_KEY="REPLACE_WITH_YOUR_KEY" \
-e TP_AGENT_ALIAS="My First Agent" \
testproject/agent:latest

But its supposed to be a multiline command, and I copied the command line by line and pressed enter after every line and bam! it worked.

Sometimes when you copy off of the web, the new-line character gets omitted, hence my suggestion to try manually introducing the new line.

Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
7

In my case, the command was like this

 docker run -d --name kong-database \
  --network=kong-net \
  -p 5432:5432 \
  -e "POSTGRES_USER=kong" \
  -e "POSTGRES_DB=kong" \
  -e "POSTGRES_PASSWORD=kongpass" \
  postgres:9.6

I was trying to copy everything in a single line including slash (\) and was getting the following error

docker: invalid reference format.

The slash (\) in the command indicates the next line, so if you are trying to run the complete command in a single line just remove the slash (\), it worked for me

Thanks :)

  • I didn't remove the slash, but your answer did give me some insights what went wrong. because backslash (\) was expecting new line, if you have space it will not recognized by docker. Just remove extra spaces after backslash(\), and copy paste will work – Rosemary Jan 11 '23 at 18:23
5

I was using a very generic command

docker build .

It turned out that I needed to just specify the tag name otherwise there was no name to use

docker build . -t image_name

And then it worked.

Cameron
  • 2,805
  • 3
  • 31
  • 45
3

This also happens when you use development docker compose like the below, in production. You don't want to be building images in production as that breaks the ideology of containers. We should be deploying images:

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"

Change that to use the built image:

  web:
    command: /bin/bash run.sh
    image: registry.example.com:9000/dyndns_api_web:0.1
    ports:
      - "8000:8000"
tread
  • 10,133
  • 17
  • 95
  • 170
3

Found that using docker-compose config reported what the problem was.

In my case, an override compose file with an entry that was overriding nothing.

3

I removed () and worked for me like this docker run -d -v $pwd/envoy.yaml:/etc/envoy/envoy.yaml:ro -p 8080:8080 -p 9901:9901 I am using windows 10

Akif Kara
  • 442
  • 6
  • 14
3

Sometimes the problem is an additional whitespace at the end of line.

Bad:

-v "$(pwd)/../src:/src" \

Spaces at the end!

Good:

-v "$(pwd)/../src:/src" \

Only newline after the slash!

Bonus hint: Some editors (like VSCode) give a different color to a slash followed with a whitespace.

Michał Słapek
  • 1,112
  • 7
  • 10
2

I was getting the same issue while using $(pwd) through powershell, I replaced it with ${pwd} and it worked for me.

Following are the details for the same:

PS C:\Users\Systems\git\mastery\dockerfile> docker container run -d --name nginx -p 8080:80 -v $(pwd):/usr/share/nginx/html nginx
docker: invalid reference format.
See 'docker run --help'.

PS C:\Users\Systems\git\mastery\dockerfile> docker container run -d --name nginx -p 8080:80 -v ${pwd}:/usr/share/nginx/html nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
Digest: sha256:25975292693bcc062becc20a1608936927f9950a42fb4474088f6292dacd4ec9
Status: Downloaded newer image for nginx:latest
f6ff08ed3b73095e85474bd2f248a7cae6b5bcdb53e12e4824d235df3cd268aa
Hanamant Jadhav
  • 627
  • 7
  • 12
2

In my case the problem is in the full path to folder with repo where I use terminal & docker. I was something like this: repo (1)/repo/ so rename folder to repo/repo and it will help

Yauheni Leaniuk
  • 418
  • 1
  • 6
  • 15
2

In my case the reason was invisible characters after copy/paste from Notes. Replacing them with spaces helped

enter image description here

Denis
  • 759
  • 1
  • 9
  • 22
0

One of the problems I had is that in my configs of skaffold config list -a, my default-repo was set to https://docker-registry.url.com as seen below:

kubeContexts:
- kube-context: minikube
  default-repo: https://docker-registry.url.com

but instead, the correct way should have been without https://:

kubeContexts:
- kube-context: minikube
  default-repo: docker-registry.url.com
aks
  • 554
  • 6
  • 8
0

Run it on CMD instead of PowerShell if you are using Windows.

lioune Sr
  • 9
  • 1
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 09 '22 at 12:15
  • in CMD its %CD% not PWD – Augustas Feb 20 '23 at 08:59
0

In case anyone else hits it, yet another variant that results in this error is caused by a trailing / or \ on your path specification.

i.e. -v c:\mystuff:c:\mappedfolder works

but: -v c:\mystuff\:c:\mappedfolder does not

Where there is the extra "\" at the end of mystuff

Steve
  • 976
  • 12
  • 12
0

So in my case, the problem was that the command was "illegal", aka. in a format that was not supported, which was the last thing expected since I found it in the official documentation, but it is what it is. Make sure you are using the right command.

0

Another case when this can show up is when you're missing the image to be run.

Vlad A. Ionescu
  • 2,638
  • 1
  • 16
  • 19
0

Just a heads-up: if any folder of you path has a space in it, remove it. For example if one of the folder's name is my folder, change it into my-folder.

Angelo Badellino
  • 2,141
  • 2
  • 24
  • 45
0

If you run the script in windows PowerShell you need to replace $(pwd) to ${pwd} with curly brackets.

0

Remove the \ and just write your command continuously.

Write it like this

docker run -p 8888:8888 -v `pwd`/../src:/src -v `pwd`/../data:/data -w /src supervisely_anpr --rm -it bash

instead of

docker run \
    -p 8888:8888 
    -v `pwd`/../src:/src \
    -v `pwd`/../data:/data -w /src supervisely_anpr \
    --rm \
    -it \
    bash
buddemat
  • 4,552
  • 14
  • 29
  • 49
0

I had a scenario where a note program had replaced -- (0x2D0x2D) with (0x2014). When I spotted an invalid switch, I typed an extra - but ended up with —- (0x20140x2D) and not 0x2D0x2D

Adam C
  • 86
  • 1
  • 4
0

In my case, the issue was empty space after the '\'. Copy and paste the whole command into an editor configured to show the hidden characters (VSCode, Notepad++, sublime) and choose the file type to be bash the editor will show the incorrect '\'

Sublime showing the extra space and incorrect backslash