293

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.

7
  • see if the following helps you stackoverflow.com/a/45281200/1398418 Commented Aug 14, 2017 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 Commented Aug 14, 2017 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. Commented Aug 14, 2017 at 21:28
  • 5
    considering posting an answer to this so is easier to find for the next guy Commented Feb 15, 2018 at 9:58
  • 10
    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. Commented Jun 21, 2019 at 18:17

30 Answers 30

298

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

Sign up to request clarification or add additional context in comments.

6 Comments

THIS is what caused me to bang my head into the wall (sometimes I hate M$FT)
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.
For anyone having problem like @paul for a perfectly correct command, Please refer to this answer stackoverflow.com/a/65690853/807104
Why does this work? $(pwd) and ${pwd} seem to output the exact same thing when I run the two commands in Powershell.
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
|
88

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

Good Luck...

6 Comments

In my case it was the issue of -- and quotes converted to some fancy utf-8 characters that docker did not recognised.
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.
Thanks you, for me it was the " \ " caracter of the n8n documentation that I deleted
ok, that solved my issue...
The what..? Haha easy to miss this, nice one!
|
76

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

2 Comments

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.
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.
18

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.

3 Comments

I had the same problem and I fixed it by setting the missing environment variable export tag=latest
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.
I also had this error where I accidentally typed "example.org:foo/bar:latest" instead of "example.org/foo/bar:latest"
16

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.

Comments

13

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

Comments

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

1 Comment

This also worked for me on Windows CMD when I tried "%cd%:/path/to/go"
11

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.


I encountered this another instance, I was trying to execute the below code on windows

docker run -it -p 7860:7860 --platform=linux/amd64 \
    registry.hf.space/fffiloni-videoretalking:latest 

This time it was the linebreak was wrong (Linux), so the correct command was

docker run -it -p 7860:7860 --platform=linux/amd64 --gpus all `
  registry.hf.space/fffiloni-videoretalking:latest

Comments

9

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

1 Comment

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
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.

1 Comment

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.
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.

Comments

4

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

enter image description here

1 Comment

is really denis exist other than
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.

Comments

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

Comments

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"

Comments

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

Comments

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

Comments

2

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

Comments

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

Comments

0

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

2 Comments

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.
in CMD its %CD% not PWD
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

Comments

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.

Comments

0

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

Comments

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.

Comments

0

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

Comments

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

Comments

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

Comments

0

docker run -p ${C_PORT}:22 --name ${CONTAINER_NAME} ${IMAGE_ID} /bin/bash

Either both CONTAINER_NAME and IMAGE_ID must be asserted. Or, run w/ sudo.

Comments

0

I've encountered this using docker compose.

The issue was in my docker-compose.yml:

  foobar:
    hostname: foobar
    container_name: "foobar "
    image: "foobar :latest"
    networks:
      - foonet
    ports:
      - "1002:1002"

There are two copy/paste whitespaces that resulted in the invalid reference format error message. Correcting the entry to:

  foobar:
    hostname: foobar
    container_name: "foobar"
    image: "foobar:latest"
    networks:
      - foonet
    ports:
      - "1002:1002"

Fixed the error.

Comments

0

I ran into this issue when I was attempting to run a remotely hosted image, i.e. docker run --platform=linux/amd64 --name LocalMSSQL -e ACCEPT_EULA=1 -e MSSQL_SA_PASSWORD=Passw0rd! -p 1433:1433 -d http://mcr.microsoft.com/mssql/server:2022-latest, and it complained because of the http:// portion. Removing that worked.

This however worked: docker run --platform=linux/amd64 --name LocalMSSQL -e ACCEPT_EULA=1 -e MSSQL_SA_PASSWORD=Passw0rd! -p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.