Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker build command add 'C:/Program Files/Git' to the path passed as build argument when executed in MINGW bash on Windows

I have following Dockerfile:

FROM ubuntu:16.04

ARG path1=def_path1
RUN mkdir ${path1}

When I build this Dockerfile using following command:

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

I get following error when I execute it in MINGW bash on Windows 10:

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

Building same Dockerfile in Windows Command Prompt or on Linux or Mac is ok. The problem is only in MINGW bash terminal on Windows because it adds 'C:/Program Files/Git' before the path that is passed as argument.

Is there a way to execute this in MINGW bash so it does not add the 'C:/Program Files/Git' prefix?

Thanks

like image 861
Dragan Nikolic Avatar asked Jan 24 '18 16:01

Dragan Nikolic


People also ask

What is the build command for docker?

The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

What happens when the command docker build is executed?

The build command is used to build an image from a Dockerfile, but the command has to be run in the same directory as the Dockerfile. When an image is built, the commands specified in the Dockerfile are executed. The operating system is installed​ along with all the packages required in the Docker container.

Can I run docker commands from Git bash?

Even though you run your docker executable in "git bash" the underlying executable is still a windows version of docker which makes it hiccup. On Powershell this works because Powershell creates the path as it should (windows version) on CMD the shell does not understand this command. and it might actually work.


2 Answers

This is actually a bug/limitation of Git for Windows as described in the Release Notes under Known issues:

If you specify command-line options starting with a slash, POSIX-to-Windows path conversion will kick in converting e.g. "/usr/bin/bash.exe" to "C:\Program Files\Git\usr\bin\bash.exe". When that is not desired -- e.g. "--upload-pack=/opt/git/bin/git-upload-pack" or "-L/regex/" -- you need to set the environment variable MSYS_NO_PATHCONV temporarily, like so:

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

Alternatively, you can double the first slash to avoid POSIX-to-Windows path conversion, e.g. "//usr/bin/bash.exe".

like image 149
mat007 Avatar answered Oct 07 '22 19:10

mat007


Further to @mat007's answer:

This bash function solved the problem more permanently for docker, without enabling MSYS_NO_PATHCONV globally, which causes another world of pain.

.bashrc

# 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" "$@")
}

You may need to do the same for docker-compose

like image 34
Bae Avatar answered Oct 07 '22 17:10

Bae