Docker in Windows Moving A Mounting
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:
1 | docker run - v $( pwd ): /var/myproject -w /var/myproject someimage:latest somecommand |
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/myproject
on the target container-w /var/myproject
– given I mounted my working directory, let’s make it the working directory of the running containersomeimage:latest
– replaced with the actual image – e.g.node:10.6.1
or somesuchsomecommand
– whatever we’re running on the working directory – e.g.npm install
or 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.C:/foo/bar/
/var/myproject
on 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)
=>$(pwd -W)
– the-W
switch makes bash use the windows native path, not the unixey alternative/var/myproject
=>//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.
Published on System Code Geeks with permission by Ashley Frieze, partner at our SCG program. See the original article here: Docker in Windows Moving A Mounting Opinions expressed by System Code Geeks contributors are their own. |