113

I've found some interesting weirdness when trying to mount a docker image on windows.

I created a .sh script that does a mount of the project folder to run our developer environment image. I want one script that every dev can run, regardless of their machine. All it does is runs docker with the current project folder.

#!/usr/bin/env bash
docker run -it --rm -v D:\my\project\folder:/wkDir $IMAGE_TAG yarn dev

Runs okay. Now the plan is to call this script from npm, so I'd like this to work relative to the current folder. Let's try another version.

docker run -it --rm -v $PWD:/wkDir $IMAGE_TAG yarn dev

Fails with:

C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from 
daemon: Mount denied:
The source path "D:/my/project/folder;C"
doesn't exist and is not known to Docker.

Wat. What's ;C and where did it come from?

So I do echo $PWD which gives me /d/my/project/folder.

Interesting, so $PWD resolves to the correct path in linux path format, and it seems like docker is trying to translate from that to the correct windows path, except there's this ;C that appears out of nowhere. And the \ are /...

What exactly is going on here?

I get the same result in VSCode's terminal git bash and powershell.

Update: I noticed that running the .sh in VSCode's powershell terminal, opens a separate cmd.exe console window which seems to run the script in git bash. So this might be a git bash issue.

7 Answers 7

164

So with some extra digging I found these three threads, related to git-bash mucking up docker mount:

https://forums.docker.com/t/weird-error-under-git-bash-msys-solved/9210 https://github.com/moby/moby/issues/24029#issuecomment-250412919

When I look up mingw's documentation on the path conversion git-bash is using, I find this table of syntax: http://www.mingw.org/wiki/Posix_path_conversion

One of which outputs in the format: x;x;C:\MinGW\msys\1.0\x. Note the ;C in it. If git-bash is trying to be clever, stuffing up the syntax and outputting a path with this format, this would explain it.

Solution is to escape the path conversion, using by prefixing with /. So the working docker command to run docker from git-bash with present working directory:

docker run -it --rm -v /${PWD}:/wkDir $IMAGE_TAG yarn dev
7
  • 11
    I found that I had to additionally wrap the path into a string so "/${PWD}" Aug 28, 2018 at 8:57
  • 13
    $ docker run -p 8080:3000 -v /$(pwd):/var/www -w //var/www node npm start I eventually found that I had to use the leading slash with parenthesis instead of braces. Also, with the working directory I needed two leading slashes. FYI: this is the command I needed for Docker for Web Developers on Pluralsight
    – a2k42
    Oct 10, 2018 at 15:35
  • 2
    This worked for me (on Windows 10 git bash): docker run --name my-wordpress -v "/${PWD}/wordpress":/wordpress_sources -p 80:80 -d k <image_name>
    – progonkpa
    Nov 10, 2018 at 9:08
  • 4
    Life saving. Windows10 and git-bash here, took me a long while to try mounting volume without using docker-compose, until I see this post. Now this works: docker run --rm -v /${PWD}/migrations:/flyway/sql --network xxx_default flyway. Thank you.
    – Emily
    Mar 20, 2019 at 14:53
  • 1
    LIFE. SAVER. +1000000 Nov 7, 2019 at 19:44
19

Mounting the current directory into a Docker container in Windows 10 from Git Bash (MinGW) may fail due to a POSIX path conversion. Any path starting with / is converted to a valid Windows path.

touch test.txt
docker run --rm -v $(pwd):/data busybox ls -la /data/test.txt
# ls: C:/Git/data/test.txt: No such file or directory

Escape the POSIX paths by prefixing with /

To skip the path conversion, all POSIX paths have to be prefixed with the extra leading slash (/), including /$(pwd).

touch test.txt
docker run --rm -v /$(pwd):/data busybox ls -la //data/test.txt
# -rwxr-xr-x    1 root     root             0 Jun 22 23:45 //data/test.txt

In Git Bash the path //data/test.txt is not converted and in Linux shells // (leading double slash) is ignored and treated the same way as /.

Disable the path conversion

Disable the POSIX path conversion in Git Bash (MinGW) using MSYS_NO_PATHCONV environment variable.

The path conversion can be disabled at the command level:

touch test.txt
MSYS_NO_PATHCONV=1 docker run --rm -v $(pwd):/data busybox ls -la /data/test.txt
# -rwxr-xr-x    1 root     root             0 Jun 22 23:45 /data/test.txt

The path conversion can be disabled at the shell (or system) level:

export MSYS_NO_PATHCONV=1
touch test.txt
docker run --rm -v $(pwd):/data busybox ls -la /data/test.txt
# -rwxr-xr-x    1 root     root             0 Jun 22 23:45 /data/test.txt
2
  • 2
    Thanks for providing this solution as escaping didn't work for me, while disabling the path conversion did. Aug 14, 2020 at 8:55
  • Maybe the disabling of the path conversion should be listed first, for lazy people who just copy/paste the first solution :p Also, for me the command-level variable setting did not work on Git Bash in Windows10: "bash: MSYS_NO_PATHCONV=1: command not found" So the proper solution seems to be the one with export MSYS_NO_PATHCONV=1 Apr 6, 2021 at 18:42
3

For me the solution was simply to include a closing slash / at end of any paths.

E.g. instead of

/opt/apache-atlas-2.0.0/bin/atlas_start.py

...use

/opt/apache-atlas-2.0.0/bin/atlas_start.py/

1
  • 1
    This is very misleading. A path ending with slash means it's a directory. It doesn't make sense to add a slash after a python file (or any file which is not a directory) ! Apr 6, 2021 at 18:39
2

I had the same issue on git bash and not command prompt. You can instead

docker run -it --rm -v "/${PWD}/D:\my\project\folder":/wkDir $IMAGE_TAG yarn dev
1
  • just -v "c:\my\path":wkdir is enough in Docker Desktop 3.2.2 Apr 16, 2021 at 20:31
0

Can you try below command -

docker run -it --rm -v %cd%:/wkDir $IMAGE_TAG yarn dev
0

I've actually had the same issue. Depending on if you are using Git Bash this command works(using nginx as an example):

docker container run --name container-name -v `pwd -W` /html:/usr/share/nginx/html -p 8000:80 -d nginx

of course you can specify the port and directory as you desire.

0

Straight worked for me below. just don't use dynamic variable.

docker run --rm -u root -p 8080:8080 -v jenkins-data/:/var/jenkins_home -v /var/run/docker.sock/:/var/run/docker.sock -v /Users/<YOUR USER NAME>/:/home jenkinsci/blueocean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.