for-win icon indicating copy to clipboard operation
for-win copied to clipboard

[WSL2] Mounting linux path as volume not working

Open verglor opened this issue 7 years ago • 62 comments

  • [x] I have tried with the latest version of my channel (Stable or Edge)
  • [x] I have uploaded Diagnostics
  • Diagnostics ID: 83925FF1-D3EE-4CBF-A54A-68AC6D60F69F/20191018171529

Expected behavior

Mounting linux path (e.g. /etc/timezone) as volume should mount that path from within wsl machine.

Actual behavior

The path c:\etc\timezone (/mnt/c/etc/timezone within wsl machine) is actually mounted as volume. If it does not exist it creates it as empty folder.

Information

  • Windows Version: Win 10 Pro x64 Insider Preview 18985
  • Docker Desktop Version: 2.1.4.0
  • Are you running inside a virtualized Windows e.g. on a cloud server or on a mac VM: no
  • WSL machine: Ubuntu 18.04

Steps to reproduce the behavior

> docker context use wsl
wsl
Current context is now "wsl"
> wsl cat /etc/timezone
Europe/Bratislava
> docker run --rm -it -v /etc/timezone:/etc/timezone alpine cat /etc/timezone
cat: read error: Is a directory
> # previous command created empty folder "c:\etc\timezone"
> rmdir c:\etc\timezone
> echo FAIL > c:\etc\timezone
> cat c:\etc\timezone
FAIL
> wsl cat /mnt/c/etc/timezone
FAIL
> docker run --rm -it -v /etc/timezone:/etc/timezone alpine cat /etc/timezone
FAIL

Notes

Be able to mount linux paths from within wsl/virtual machine is essential to be able to use my containers that I am already using in linux in production, since all of them mounts at least /etc/timezone and /etc/localtime to inherit timezone configuration of the host.

verglor avatar Sep 26 '19 15:09 verglor

cc @simonferquel

stephen-turner avatar Sep 26 '19 15:09 stephen-turner

Thanks for reporting this. It will be fixed in an upcoming release

simonferquel avatar Sep 26 '19 18:09 simonferquel

Hi @simonferquel, I just tested it with 2.1.4.0 and the problem persists.

verglor avatar Oct 15 '19 18:10 verglor

This appears to still be an issue on 2.1.7.0 (41536). Is there a path towards resolution on this? This is kind of a deal-breaker?

john-landgrave avatar Dec 30 '19 16:12 john-landgrave

Just to add, according to:

https://www.docker.com/blog/new-docker-desktop-wsl2-backend/

Under the Initial limitations section.

It reads:

You can only mounts files backed by your distro VHD (that means you can’t bind mounts things within /tmp, /mnt, /var/run, /proc, /sys etc.). For most people, this should not be a problem, but mounting things like /var/run/docker.sock in a container won’t work initially. We are working with Microsoft on solving this issue, a future Windows Update will bring full bind mount support.

But as /etc/ is backed by the distro VHD, this is surely a bug?

Oh, and it's still broken in 2.2.2.0 (43066)

flatline-studios avatar Mar 03 '20 17:03 flatline-studios

This is no more a limitation. Are you running the docker cli from your wsl distro?

simonferquel avatar Mar 03 '20 17:03 simonferquel

This is no more a limitation. Are you running the docker cli from your wsl distro?

This was never an issue if running docker commands inside the WSL 2 VM, I mean, why would it? But that defeats the real point of having WSL 2 with the improved Docker compatibility in the first place.

This issue is about not being able to run:

docker run --rm -it -v /etc/timezone:/etc/timezone alpine cat /etc/timezone

From a Windows command prompt, and for it to bind the /etc/timezone file from the WSL 2 environment, in to the container.

flatline-studios avatar Mar 03 '20 19:03 flatline-studios

To be clear, the issue that @flatline-studios presented is not the problem I'm having. I'm attempting to do something like:

docker run -v ~/apps/test-app:/var/www/html -name testWp -d wordpress

And it generates the container, but the contents of the /var/www/html folder in the container are not the contents of ~/apps/test-app, verified by doing a docker exec -it testWp /bin/bash and the poking around in there.

john-landgrave avatar Mar 03 '20 19:03 john-landgrave

To be clear, the issue that @flatline-studios presented is not the problem I'm having. I'm attempting to do something like:

docker run -v ~/apps/test-app:/var/www/html -name testWp -d wordpress

And it generates the container, but the contents of the /var/www/html folder in the container are not the contents of ~/apps/test-app, verified by doing a docker exec -it testWp /bin/bash and the poking around in there.

Just to clarify...

  1. You're running that command in Windows command line (not in WSL itself)?
  2. The ~/apps/test-app directory, that's stored within the WSL filesystem, is not being mounted in to /var/www/html, within the wordpress container?

If the answer is yes to both of those, then we're describing the same issue.

flatline-studios avatar Mar 03 '20 19:03 flatline-studios

  1. I am running this command from within WSL2 (which docker returns /usr/bin/docker)
  2. That is correct.

john-landgrave avatar Mar 03 '20 20:03 john-landgrave

@john-landgrave I can't reproduce on latest edge release, directory is correctly mounted and creating/modifying files from the wsl distro reflects in the container. Can you check the latest release ?

simonferquel avatar Mar 04 '20 08:03 simonferquel

@simonferquel - I'm not sure if I'm being clear enough about the issue that still exists.

C:\Development
λ docker run --rm -it -v /etc/timezone:/etc/timezone alpine cat /etc/timezone
cat: read error: Is a directory

C:\Development
λ wsl -- docker run --rm -it -v /etc/timezone:/etc/timezone alpine cat /etc/timezone
Europe/London

C:\Development Is the directory my Windows Shell is currently in.

λ Is my command prompt symbol (using CMDer).


It seems that Docker for Windows isn't smart enough to look at the local mount directory /etc/timezone, realise that this is a Unix-based directory, and attempt to mount the directory from within the WSL machine.

A simple regex check ^\/[^\/].* (https://regex101.com/r/1kO0AE/1) on the host mount directory should be sufficient to do the job (granted, I haven't thought much about all the niche cases, so the regex might not suitable, as-is).


My workaround at the moment is to just use two alias' in the Windows Shell:

alias docker=wsl -- docker $*
alias docker-compose=wsl -- docker-compose $*

Which then produce:

C:\Development
λ alias
Use /? for help

docker-compose=wsl -- docker-compose $*
docker=wsl -- docker $*

C:\Development
λ docker run --rm -it -v /etc/timezone:/etc/timezone alpine cat /etc/timezone
Europe/London

This is a very messy hack though, and I can imagine spending an hour pulling my hair out in a couple of months time, trying to work out why the following isn't working:

C:\Development
λ type C:\tmp\test.txt
test text

C:\Development
λ docker run --rm -it -v C:\tmp:/tmp/tmp alpine cat /tmp/tmp/test.txt
docker: Error response from daemon: mkdir C:tmp: permission denied.
See 'docker run --help'.

It would be looooooovely if Docker for Windows could just work out which filesystem we wanted to mount the directory from in the first place.

flatline-studios avatar Mar 04 '20 13:03 flatline-studios

@simonferquel I'm not showing any further updates presently and here's my version info: image

This still isn't working for me but is acting weird. Here's an account of the commands that I ran (all from within a WSL shell) and the output I got:

docker run --name test -v ~/apps/test:/var/www/html -e MYSQL_ROOT_PASSWORD=##### -d --rm wordpress

Where ~/apps/test is a wordpress directory on my WSL2 instance and the real password was dropped in for the #####. I also tried this without the -d and saw the following message:

WordPress not found in /var/www/html - copying now...

Which is weird because that directory, which was mounted to /var/www/html definitely has WordPress in it. So I decided to try to debug this by mounting it to a different spot: /var/test.

docker stop test
docker rm test
docker run --name test -v ~/apps/test:/var/test -e MYSQL_ROOT_PASSWORD=##### -d --rm wordpress

Here's where things get weird. When I docker exec -it test /bin/bash into the container and do an ls /var/test the contents of that directory are identical to /var/www/html from the previous trial, and still not the directory that I'm trying to mount. So, I decided to try a fresh directory that I've never mounted and just drop it into /var/app, a directory which doesn't exist by default on the WordPress container:

docker stop test
docker rm test
docker run --name test -v ~/apps/other-app:/var/app -e MYSQL_ROOT_PASSWORD=##### -d --rm wordpress

After docker exec -it test /bin/bash'ing into the container and doing an ls /var/ I see that /var/app does indeed exist (yay!). However, when I ls /var/app the directory is empty...

My next thought was that somehow it was caching volumes that were being created or something like that? So I looked at docker volume ls but I didn't see anything suspicious there.

For reference, I have also tried absolute paths on the left side of all of the -v arguments (e.g. /home/my-user/other-app).

I have also tried rebooting, factory resetting, and checking for updates after each of those things. Any ideas?


@flatline-studios I think that we're definitely talking about two distinct issues, and I can't necessarily determine which one the original post is talking about (unless it's a third issue with a similar enough sounding description). If I need to make a new issue (@simonferquel ?) then I'm happy to, just don't want to muddy things up too much.

john-landgrave avatar Mar 04 '20 14:03 john-landgrave

Also, my diagnostics just finished @simonferquel and here is my diagnostics ID: B0076B2C-6E30-4CE4-B2B4-069CD453F0D3/20200304144117

john-landgrave avatar Mar 04 '20 14:03 john-landgrave

@flatline-studios unfortunately we can't do that: we support integration with multiple distros, and we can't decide which to mount from. Also, historically, unix paths in mount sources from Windows means "mount something from the linuxkit environment" (which makes things like docker run -v /var/run/docker.sock:/var/run/docker.sock work). So we don't want to potentially break existing workflows there.

@john-landgrave I'll have a look at the diagnostic file.

simonferquel avatar Mar 04 '20 15:03 simonferquel

@john-landgrave it might be related to a bug I fixed recently. Can you try this private build: https://download-stage.docker.com/win/edge/43174/Docker%20Desktop%20Installer.exe ?

simonferquel avatar Mar 04 '20 15:03 simonferquel

@simonferquel out for lunch right now, but I'll download it give it a shot when I get back in about an hour.

john-landgrave avatar Mar 04 '20 16:03 john-landgrave

@flatline-studios unfortunately we can't do that: we support integration with multiple distros, and we can't decide which to mount from. Also, historically, unix paths in mount sources from Windows means "mount something from the linuxkit environment" (which makes things like docker run -v /var/run/docker.sock:/var/run/docker.sock work). So we don't want to potentially break existing workflows there.

@simonferquel I appreciate that, and thanks for clearing up the reasoning behind it. Much appreciated.

Trying to find a better solution than my aliases, I tried using the \\wsl$\Ubuntu-18.04\etc\timezone, but hit issue #5888.

Is there a simpler way to get around this? Like, instead of making Docker for Windows smarter, as I suggested with the regex... Wouldn't it be much easier to just tell Docker what we want. Like:

docker run --rm -it -v wsl_default//etc/timezone:/etc/timezone alpine cat /etc/timezone
                       ^^^^^^^^^^^^
docker run --rm -it -v wsl_Ubuntu-18.04//etc/timezone:/etc/timezone alpine cat /etc/timezone
                       ^^^^^^^^^^^^^^^^^

docker run --rm -it -v ?_default//etc/timezone:/etc/timezone alpine cat /etc/timezone
                       ^^^^^^^^^^
docker run --rm -it -v ?_Ubuntu-18.04//etc/timezone:/etc/timezone alpine cat /etc/timezone
                       ^^^^^^^^^^^^^^^

docker run --rm -it -v default//etc/timezone:/etc/timezone alpine cat /etc/timezone
                       ^^^^^^^^
docker run --rm -it -v Ubuntu-18.04//etc/timezone:/etc/timezone alpine cat /etc/timezone
                       ^^^^^^^^^^^^^

Or something similar?

Using // between the WSL VM distro and the directory to mount inside it seemed like a good idea. As / is an illegal character in both Windows and *nix based directories and filenames, I can't see a situation where // could appear without throwing an error under normal circumstances, so should make the ideal separator?


You mentioned the need to support multiple distros, and forgive my ignorance (I'm not well versed in the lower level Docker for Windows workings), but would that still be a problem with my proposed solution(s)?

flatline-studios avatar Mar 04 '20 17:03 flatline-studios

@simonferquel no dice: still have the same issue repeating the steps from my previous post.

If it helps, when I just mounted ~/apps after trying to specifically mount ~/apps/test and inspected /var/apps there was only one folder in there: test. I then Restarted docker (using the option in the menu on the tray) and executed only the mount -v /home/my_user/apps:/var/apps and the directory was empty inside the container (that directory has a lot of stuff in it, if that matters).

Should I uninstall Docker before installing the build that you sent over? It seemed to update to 2.2.3.0 fine?

image

john-landgrave avatar Mar 04 '20 18:03 john-landgrave

Upgrading should be ok. Can you send a new diagnostic ? (This build as slightly more verbose logs)

simonferquel avatar Mar 04 '20 22:03 simonferquel