简体   繁体   中英

Can I use Docker's Linux filesystem in WSL 2 to bind mount a directory instead of installing a Linux distro?

I'm using Docker via WSL2 on Windows 10. I only want to be able to create Linux containers and map a directory in the container to a WSL directory.

I'm pretty sure WSL must already have access to a Linux filesystem in order to create any container volumes used, so if I want to bind mount a directory can I use that filesystem instead of having to install and host an entire separate Linux distro?

I've already found the filesystem Docker uses at \wsl$\docker-desktop and tried to bind mount directories in there, but not managed to get this to work.

Does anyone know how to get this to work, or why it won't?

UPDATE Here is a session of me creating a file in the WSL filesystem and bind mounting it into a Docker container. All commands complete without error, but it just doesn't work, inside the container no changes to the bind mount are detected.

:: Verify existing distros and default distro
C:\Users\User>wsl -l
Windows Subsystem for Linux Distributions:
docker-desktop (Default)
docker-desktop-data

:: Start wsl
C:\Users\User>wsl
DESKTOP-SDE0C3N:/tmp/docker-desktop-root/mnt/host/c/Users/User#

:: View filesystem mounts
DESKTOP-SDE0C3N:/tmp/docker-desktop-root/mnt/host/c/Users/User# cd ../../../
DESKTOP-SDE0C3N:/tmp/docker-desktop-root/mnt/host# ls
c    wsl

:: Create a directory in the WSL filesystem to bind mount into a docker image
DESKTOP-SDE0C3N:/tmp/docker-desktop-root/mnt/host# mkdir wsl/test-dir1
DESKTOP-SDE0C3N:/tmp/docker-desktop-root/mnt/host# ls wsl
docker-desktop       docker-desktop-data  test-dir1

:: In a Windows CMD shell spin up a docker container with a bind mount to the created directory
C:\Home>docker run --rm -it --entrypoint bash -v //wsl$/test-dir1:/myapp node
root@3a56694db873:/#

:: In the container verify the bind mount is present - 'myapp' is there
root@3a56694db873:/# ls
bin   dev  home  lib64  mnt    opt   root  sbin  sys  usr
boot  etc  lib   media  myapp  proc  run   srv   tmp  var

:: Back in the WSL Bash prompt (NOT in the container) create a file in the supposedly mounted directory.
DESKTOP-SDE0C3N:/tmp/docker-desktop-root/mnt/host/wsl/test-dir1# echo Hi!! > test1.txt
DESKTOP-SDE0C3N:/tmp/docker-desktop-root/mnt/host/wsl/test-dir1# ls
test1.txt

:: In the interactive shell attached to the container verify that the created file is visible.
root@3a56694db873:/# ls myapp
root@3a56694db873:/#        <-- test1.txt should be here!!!

The created file is not seen from within the container. The bind mount is not working.

That won't work for several reasons.

The purpose of bind mounting a directory in the Linux filesystem to a Docker Container is that on Windows this provides much better IO performance and working file update notifications than bind mounting a directory on the Windows filesystem.

However for the bind mount to work the docker run command must be invoked from within the Linux distro in order that the bind mount path is a valid Linux path in that distro.

The docker-desktop distro cannot do this for two reasons.

  1. The standard WSL command interop that enables Windows commands to be run within the filesystem of a Linux distro do not work on the docker-desktop distro because it's a special distro only intended to function as part of the Docker implementation. So commands like cmd.exe /C dir which would work fine in a standard WSL Linux distro will not work in the docker-desktop distro. So the Windows docker.exe also cannot be run in the docker-desktop distro for the same reason.

  2. Docker Desktop has an integration setting which can be enabled to add docker support for a specified WSL Linux distro, when enabled this it what enables docker commands to be executed from within that distro. However Docker Desktop does not enable this option on its own distro and there is no way to enable it.

So that why you have to install an additional Linux distro and bind mount container directories from that instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM