docker 构建命令将'C:/Program Files/Git'添加到在 Windows 上的 MINGW bash 中执行时作为构建参数传递的路径



我有以下Dockerfile:

FROM ubuntu:16.04
ARG path1=def_path1
RUN mkdir ${path1}

当我使用以下命令构建此 Dockerfile 时:

docker build --build-arg path1=/home/dragan -t build_arg_ex .

当我在 Windows 10 上的 MINGW bash 中执行它时,我收到以下错误:

$ ./build.sh --no-cache
Sending build context to Docker daemon  6.144kB
Step 1/3 : FROM ubuntu:16.04
 ---> 2a4cca5ac898
Step 2/3 : ARG path1=def_path1
 ---> Running in a35241ebdef3
Removing intermediate container a35241ebdef3
 ---> 01475e50af4c
Step 3/3 : RUN mkdir ${path1}
 ---> Running in 2759e683cbb1
mkdir: cannot create directory 'C:/Program': No such file or directory
mkdir: cannot create directory 'Files/Git/home/dragan': No such file or 
directory
The command '/bin/sh -c mkdir ${path1}' returned a non-zero code: 1

在Windows命令提示符下或在Linux或Mac上构建相同的Dockerfile是可以的。问题仅在Windows上的MINGW bash终端中,因为它在作为参数传递的路径之前添加了"C:/Program Files/Git"。

有没有办法在 MINGW bash 中执行此操作,这样它就不会添加"C:/Program Files/Git"前缀?

谢谢

这实际上是 Git for Windows 的错误/限制,如发行说明中的已知问题中所述:

如果指定以斜杠开头的命令行选项,POSIX 到 Windows 路径的转换将启动,例如将"/usr/bin/bash.exe"转换为"C:\Program Files\Git\usr\bin\bash.exe"。当不需要时 - 例如"--upload-pack=/opt/git/bin/git-upload-pack"或"-L/regex/" - 您需要临时设置环境变量MSYS_NO_PATHCONV,如下所示:

MSYS_NO_PATHCONV=1 git blame -L/pathconv/ msys2_path_conv.cc

或者,您可以将第一个斜杠加倍以避免 POSIX 到 Windows 路径的转换,例如"//usr/bin/bash.exe"。

进一步回答@mat007:

这个 bash 函数更永久地解决了 docker 的问题,而没有全局启用MSYS_NO_PATHCONV,这导致了另一个痛苦的世界。

.巴什尔克

# See https://github.com/docker/toolbox/issues/673#issuecomment-355275054
# Workaround for Docker for Windows in Git Bash.
docker()
{
        (export MSYS_NO_PATHCONV=1; "docker.exe" "$@")
}

您可能需要对docker-compose执行相同的操作

最新更新