Using docker in Windows is a bit like discovering that someone’s missed a vital memo. Everything nearly works as you might hope, but there are glitches all over the place.
I use bash as my shell for scripting various things we do with docker in Windows. This leads to two enormously annoying issues:
- I’ve found it very difficult to mount the working directory into the container I’ve just launched
- Giving a docker container a working directory seems to result in “The directory name is invalid”
Let’s look at what we want to do:
Let’s unpack what the above is trying to do. Note, I think the above command should work on a linux/MacOS bash, and there’s a potential variant of it that might work in PowerShell… yet, it won’t work on GitBash or similar bash shells in Windows.
-v $(pwd):/var/myproject– I do this sort of thing a lot when dockerising tools to run on my project folder, so they’re portable across machines that don’t have everything installed – it’s trying to mount the current working directory to a nice neutral path
/var/myprojecton the target container
-w /var/myproject– given I mounted my working directory, let’s make it the working directory of the running container
someimage:latest– replaced with the actual image – e.g.
somecommand– whatever we’re running on the working directory – e.g.
npm installor whatever
These Two Simple Tricks Will Blow Your Mind
You need to know why the above fails on Windows:
$(pwd)returns the path in unix style – e.g.
/c/foo/bar/where Windows docker (annoyingly) requires volume mounts in native windows – e.g.
/var/myprojecton the command line is treated as a local file no matter how you quote it, and the bash invocation refuses to let it pass to the docker runtime because it’s not a valid local path
Here’s the fix:
$(pwd -W)– the
-Wswitch makes bash use the windows native path, not the unixey alternative
//var/myproject– this double slash makes the shell agree that this can’t be a local file, so it just passes it on!
It’s trivial when you know… but annoyingly not self-evident.
I know I’ll be coming back to this post to remind myself of how to fix the above. I hope it’s been useful for you too.