55

I try to run a Python program with Docker via the Windows console (Windows 10).

I had made the Windows console be capable of Docker Hello, World!.

But when I run:

 docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:2 python test.py

I get the error:

docker: Error response from daemon: create $PWD: volume name invalid:
"$PWD" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.

See 'docker run --help'.

While running the same code via Docker Quickstart Terminal, it runs well.

I tried to, but I can't solve it. How can I do it?

xpt
  • 20,363
  • 37
  • 127
  • 216
qingkejin
  • 799
  • 1
  • 5
  • 9
  • Possible duplicate of [Mount current directory as a volume in Docker on Windows 10](https://stackoverflow.com/questions/41485217/mount-current-directory-as-a-volume-in-docker-on-windows-10) – kenorb Mar 11 '19 at 22:16

6 Answers6

109

I think, the substitution of Linux command $(pwd) in Windows is "%cd%".

So, try out following command which might help you.

docker run -it --rm --name my-running-script -v "%cd%":/usr/src/myapp -w /usr/src/myapp python:2 python test.py
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Naveen Kumar G C
  • 1,332
  • 1
  • 10
  • 12
15

Read the documentation: Manage data in containers

If you are using Docker Machine on Mac or Windows, your Docker daemon has only limited access to your OS X or Windows filesystem. Docker Machine tries to auto-share your /Users (OS X) or C:\Users (Windows) directory.

So, you can mount files or directories on OS X using:

docker run -v /Users/<path>:/<container path> ...

On Windows, mount directories using:

docker run -v /c/Users/<path>:/<container path> ...`

My Docker Machine is on Windows, so instead of ' "$PWD" ' like:

docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:2 python test.py

Use ' /c/Users/your_name ' like:

docker run -it --rm --name my-running-script -v /c/Users/cn_pa:/usr/src/myapp -w /usr/src/myapp python:2 python test.py
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
qingkejin
  • 799
  • 1
  • 5
  • 9
  • 2
    The documentation link is broken, *"Sorry, we can't find that page"*. – Peter Mortensen Jul 31 '18 at 13:15
  • The fact that you should reference absolute directory paths as `/drive_letter/first_directory/etc` is what I was looking for to allow me to push my volumes to another partition. Thank you. – Austin Heller Oct 23 '22 at 04:13
12

if you use a bash command line on windows, you will works just placing a / before $ PWD. Something like:

docker run -v /$PWD:/src
M--
  • 25,431
  • 8
  • 61
  • 93
Brian
  • 1,295
  • 14
  • 25
10

This did work for me in Powershell.

docker run -p 8080:3000 -v ${pwd}:/var/www -w "/var/www" node npm start
Hardik Raval
  • 3,406
  • 1
  • 26
  • 28
2

On Windows 10, using Cmder (cmd.exe) I came upon this error while in the getting started tutorial on this step: Part 6: Use binds mounts

Start a dev-mode container

If you are using PowerShell then use this command:

 PS> docker run -dp 3000:3000 `
     -w /app -v "$(pwd):/app" `
     node:12-alpine `
     sh -c "yarn install && yarn run dev"

But if you are using Command Prompt then use this command:

docker run -dp 3000:3000 ^
-w /app -v "%cd%":/app ^
node:12-alpine ^
sh -c "yarn install && yarn run dev"
treckstar
  • 1,956
  • 5
  • 21
  • 26
-3

Don't know if you already got it fixed with the solutions provided already but my workaround is that windows don't read bracelets $(pwd). ${pwd} did the trick for me.

achahbar
  • 901
  • 3
  • 21
  • 47