Introduction
while playing with Docker and the Named Pipe trick, I wanted to give a more "*nix" feeling of Craig Wilhite volume mounting example:$ docker run -v C:/Users/crwilhit.REDMOND/tmp/ microsoft/nanoserver cmd.exeThere's nothing wrong with this command, but the "scope" is WSL shell, and while using the docker nix command, the path of the volume is the windows one.
There should be a better way, no?
WSLENV to save the day
Luckily, the WSL team did already implement a very nice feature, WSLENV, and a linux command, wslpath that will help translating the Windows path.Now it would be up to you to either create, in Windows, an environment variable and use it.
Here is the same example, using LCOW (because why not), for mounting volumes in a "linux way":
$ docker run --platform=linux -it -v $DOCKERVOL/mydir:/sharedvol ubuntu bashor use the wslpath command:
$ docker run --platform=linux -it -v `wslpath -w /mnt/c/DockerVolumes/mydir`:/sharedvol ubuntu bash
Mixing all and having fun
Playing with the paths, is nice, but it's a tweet from the legendary Scott Hanselman that triggered an idea even crazier: try the same with windows commands in WSL shell.First of all, we need to understand what I call the "scope" between a command and the shell used to run it.
In Scott's example, he's piping a value from a windows command (dir) to a bash one (grep).
The pipe, is a Shell function, not related to the command itself.
So with that in mind, here is the example from before but with docker.exe from WSL:
$ docker.exe run --platform=linux -it -v `wslpath -w /mnt/c/DockerVolumes/mydir`:/sharedvol ubuntu bash"As we are in WSL scope, the command substitution will be interpreted successfully and then the windows command will run thanks to the interopability.
The end command would be something like:
$ docker.exe run --platform=linux -it -v C:\DockerVolumes\mydir:/sharedvol ubuntu bash
This means that you will be able to use bash variables with Windows commands too.Restrictions
With all that said, there's still some restrictions that really need to be known:- The directories have to be in the Windows visible FS. Anything in /mnt/<drive letter>/... is OK
- If you didn't mounted your WSL home then trying to use wslpath on / or /home will simply fail
- If you're using Powershell as the default, you will have to do like Scott shown in his example: put "wsl.exe" before your linux command. You need to define the scope.
- Just concerning the Docker examples, the volume sharing has to be in Windows path format for both commands (linux and windows).
See Craig's notes on his blog.
Conclusion
The more WSL is evolving, the more I see no boundaries in Interop and I'm really looking forward to the first linux and/or windows applications that would use this capability natively.
Last but not least, Interop is really a state of mind. If you can look beyond the "walls" separating both worlds and instead embrace the fact that water and oil can mix, believe me when I tell you that the fun is infinite.
Ok, and a weird mind can help having crazy ideas...
>>> Nunix Out <<<
Comments
Post a Comment