Docker volumes on WSL2 using Docker Desktop
Asked Answered
K

3

20

I'm just trying out WSL 2 with Docker for Windows and I'm having an issues with mounted volumes :

version: "3.7"

services:
    node:
        build: .
        container_name: node
        hostname: node
        volumes: 
            - ./app:/app
        stdin_open: true

the container build and start well, I access it with docker exec nicely but the /app folder inside the container isn't bound to my laptop app folder. However the right path is actually correctly mounted on the running container :

(here I do pwd on the host to if it matches perfectly with what is mounted on the container)

➜  app pwd   
/mnt/c/Users/willi/devspace/these/app

And this is screen of portainer telling me what path are mounted where in the container and everything matches.

docker volume from portainer

The file I create int he app folder on the host are not visible in the app folder of the container and vice-versa. This is weird and I don't know how to debug it.

Complementary infos:

  • Windows 10 Pro 10.0.19041
  • Docker for Windows version : 2.3.0.4
  • docker version output in WSL : 19.03.12
  • docker-compose version : 1.26.2

Thanks

Kroo answered 23/8, 2020 at 21:12 Comment(3)
Per the recommendations of guide to setup the WSL2 backend for Docker, the ideal scenario is for you to have the source code or the volumes live in the WSL filesystem. It looks like you are even starting the docker services from the host machine as well. I Would recommend just moving the whole app folder to the WSL filesystem and boot up the services from thereCaterinacatering
A quick way to check mounts are correct: docker inspect -f '{{ .Mounts }}' name-or-idLeighleigha
I used GitBash and normal Unix paths except for the drive letter like E:, like -v E:/dir/file.txt:/dir/file.txt instead of -v /e/dir...Leighleigha
P
25

As @Pablo mentioned, the Best-Practice seems to be using WSL File system for mapping Volumes.

Take a look at the Docker Documentation concerning WSL2:

Best practices

  1. To get the best out of the file system performance when bind-mounting files:
    • Store source code and other data that is bind-mounted into Linux containers (i.e., with docker run -v <host-path>:<container-path>) in the Linux filesystem, rather than the Windows filesystem.
    • Linux containers only receive file change events (“inotify events”) if the original files are stored in the Linux filesystem.
    • Performance is much higher when files are bind-mounted from the Linux filesystem, rather than remoted from the Windows host. Therefore avoid docker run -v /mnt/c/users:/users (where /mnt/c is mounted from Windows).
    • Instead, from a Linux shell use a command like docker run -v ~/my-project:/sources <my-image> where ~ is expanded by the Linux shell to $HOME.
  2. If you have concerns about the size of the docker-desktop-data VHDX, or need to change it, take a look at the WSL tooling built into Windows.
  3. If you have concerns about CPU or memory usage, you can configure limits on the memory, CPU, Swap size allocated to the WSL 2 utility VM.
  4. To avoid any potential conflicts with using WSL 2 on Docker Desktop, you must uninstall any previous versions of Docker Engine and CLI installed directly through Linux distributions before installing Docker Desktop.
Personalize answered 7/10, 2020 at 7:3 Comment(4)
After copying a file from my Windows file system into WSL under the home (~/) directory as recommended, files in that directory still aren't showing up in my container - I get an empty directory. I tried doing chmod 777 on the files, but still no luck.Extracurricular
I want to to bind-mount from the Linux filesystem, but the mounts don't work. For example, I can bind ~/somewhere, which has contents, but inside the container, there are no contents.Blynn
@Personalize I'm trying to get docker setup as a local development environment. If I host my files within WSL2 Ubuntu environment, how can I modify those files within vscode (ie. change -> save -> see changes in test environment, rinse & repeat). Initially I didn't use WSL2 volume, and each WordPress page took 90-250 seconds to load, so that wasn't usable for development. I had to get the files inside WSL2 Ubuntu (I think I used mv {source_dir} {target_dir}, down to 0.5 secs per page now, but I don't know how to setup a usable dev process now.Brazier
I agree about the raw "machine" performance of the "from WSL" approach, and I use it myself when I have a runtime inside WSL. But if my runtime is on Windows, and I need only quickly wind up some things? Then copy-pasting files into WSL every time looks like not having Docker on Windows, but having virtual Linux with Docker. Which it technically is, but only under the hood, not for the user. So, while recognizing the usefulness of this answer itself, I would like to see the place for the answer to the original question too.Leighleigha
K
5

Everything works perfectly now, it seems that my problem was that my WSL distro was still in version 1. You can verify it with the command : wsl -l -v

  NAME                   STATE           VERSION
* docker-desktop-data    Stopped         2
  docker-desktop         Stopped         2
  Ubuntu-20.04           Running         2 <- This was at 1

Upgrade to WSL2

Kroo answered 16/11, 2020 at 18:37 Comment(0)
Q
0

In my case, I faced with a slash problem:

docker run -d -it --name my-running-app -v E:/folder/example/modules:\srv\app image-name.

In -v from:to, if you use Windows, from path needs to be with /, and to path needs to be with \.

I don't really know, if it help only me, but it helped.

Quackery answered 12/1, 2024 at 15:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.