Docker 安装的卷添加; 当从 linux 样式路径转换时,从 C 到窗口路径的末端

我发现了一些有趣的怪异当试图挂载一个对接图像的窗口。

我创建了一个 .sh脚本来装载项目文件夹,以运行我们的开发人员环境映像。我想要一个脚本,每个开发人员可以运行,不管他们的机器。它所做的只是使用当前项目文件夹运行 docker。

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

跑得不错。现在计划从 npm调用这个脚本,所以我希望这个脚本能够相对于当前文件夹工作。我们试试另一个版本。

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

失败的原因:

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.

什么是 ;C? 它是从哪里来的?

所以我做 echo $PWD就得到了 /d/my/project/folder

有趣的是,$PWD解析出了 linux 路径格式的正确路径,看起来 docker 正试图从这个路径转换到正确的 windows 路径,只是这个 ;C突然出现了。\/..。

这到底是怎么回事?

我在 VSCode 的终端 git bash 和 powershell 中得到了相同的结果。

更新: 我注意到,在 VSCode 的 powershell 终端中运行 .sh,会打开一个单独的 cmd.exe控制台窗口,该窗口似乎在 git bash 中运行脚本。所以这可能是 Git bash 的问题。

32765 次浏览

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

Can you try below command -

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

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.

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

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

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/

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