On a windows machine, I have a Makefile
that contains the following instruction:
start_docker:
docker run --rm -p 8888:8888 -it -v "${PWD}":/home/docker/app my_image
When I run make start_docker
either in the PowerShell or cmd prompt, I get the following output:
docker run -p 8888:8888 -it -v :/home/docker/app my_image
(note that the output displays a blanck istead of the current path)
And then my local volume is not mounted.
However, if I paste directly docker run --rm -p 8888:8888 -it -v "${PWD}":/home/docker/app my_image
the volume is mounted (great!).
I get an error "$(PWD)" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
if I try to use it in the cmd prompt).
What should I do to start my docker image with make with the correct path in windows (it works smoothly on Linux)?
I also tried ${PWD}
and $(pwd)
, it does not change the issue.
CodePudding user response:
Make doesn't set or use the variable PWD
in any way. If it is set, it is either because something in your makefile set it, or because it was inherited from the shell that invoked make
.
If you run on a POSIX system like GNU/Linux or MacOS, then the shell will (often, but not always) set the PWD
environment variable and make
will inherit that. But it's not always right, and not always set. On Windows, the Windows shell (powershell or cmd.exe) do not set this variable, so it's never set.
In short, it's always a portability problem to use PWD
.
If you like you could switch to using the GNU make variable CURDIR
:
start_docker:
docker run --rm -p 8888:8888 -it -v "${CURDIR}":/home/docker/app my_image
which is set by GNU make
when it starts, and so always has a value.