Mounting path on wsl2

I am running Docker-Desktop with wsl2 backend. I am trying to mount a path in onto my container and edit files from the container and have that reflect onto my host filesystem. In the Docker UI the path is shown as mounted but its not mounting the files that are on my host’s filesystem

What i do is:
docker run --rm -d -v /home/my_user:/home/my_user ubuntu

in my host systems /home/my_user i have a directory called foo. It not not visible inside the docker container and also changing files does not reflect on my host system.

Now i check my host fs with:
wsl -u my_user -> cd /home/my_user
no changes to be seen.

Am i missing something?

I wonder what you mean by host fs. Why would the Windows host use linux file paths?

I thought wsl2 basically lets me run docker through a linux distro. I was under the assumption it would use linux file paths because it allows me to use the /home/my_user path with does not exist on my windows machine. Where is the mount point then?

The file system of a WSL does not share anything with the host’s filesystem. Your folder/home/my_user is not shared with the host, it only lives in the WSL distros filesystem. Windows drives are mounted into the wsl distro underneath the /mnt folder.

If you run the docker command inside a WSL distribution, you can use this command to determine your windows userprofile path: wslpath $(powershell.exe -c 'Write-Host -NoNewline ${env:USERPROFILE}').

Assumed the Windows path is C:\Users\meyay, the command will print the output /mnt/c/Users/meyay

Though, if you run your docker commands in a terminal on the host, you can directly use the Windows path.

Please be aware that file access will be much slower, if a container access folders from the host filesystem. A paid Docker Desktop subscription gives you access to synchronized file sharing, which should be much faster.

okay but i am confused about what happens then if i run -v /home/my_user:/home/my_user` from my windows terminal. The path is neither in my linux distro neither on my windows and i cant see it on docker as a volume, so where can i find it?

Great question! As it was never intended to be used like that, I never tried (and don’t feel like trying), I can only assume that it uses a path inside the docker-desktop-data distribution.

You can check by opening this UNC in the file explorer: \\WSL$\docker-desktop-data

Update: now I tried, and my assumption was wrong. I have no idea in which black hole the host side disappears. After all, It was never meant to be used like that.

When you say “host filesystem”, you mean the WSL distribution or the Windows filesystem? Do you expect to see files in your container that were in your default WSL distribution’s home or in your user’s home on your Windows host?

If WSL integration is enabled, you can indeed mount files from the WSL distribution into the containers in Docker Desktop. So if you run the docker command in your WSL distribution for which WSL integration is enabled in Docker Desktop, you should be able to use the docker command almost as if Docker was installed in that distribution. I emphesize “almost” since the daemon is still running in another distribution so container IP addresses will not be available directly.

I don’t understand this. You mean the content is not visible or the folder itself is not visible?

If files are not mounted properly, that can be a Docker Desktop bug or there is something special in that folder we can’t even think of.

Hi,
I’m curious myself because I’ve just stumbled upon it lately. I have source/project files in Windows folder because it is more convenient and it turned out that there are are problems with bind-mount volumes to Windows folder (namely with Kafka data). So I’ve asked chatGPT and it suggested to use WSL directory. Unfortunately I haven’t managed to achieve anything following its suggestions. And for example using linux style path “~/some-subdir” is translated to Windows folder, something like “C:\Users\current-user\some-subdir”. Possibly all such paths are interpreted as Windows when docker is executed in Windows terminal. Although I thought that “/home/current-user/sum-subdir” could work but then I realized there can be more Linux distros and additionally not all has to be integrated with Docker Desktop (in settings). Then again such distro dirs should be accecced as shared (SMB I believe)… but will it work better then just Windows folder I have no idea.

It looks like the first posts confused me and since I didn’t get any reply to my post, I stopped thinking about it.

The fact is that whatever source path you set, that is interpreted by the Docker Daemon, so it will be the path on the machine on which the Docker daemon is running. The client doesn’t matter. That is what you run on Windows, not Docker, just the Docker client when running Linux containers.

The only magic is that Docker Desktop knows it is running a virtual machine for the Docker Daemon, so when you want to mount a folder from your Windows host, it can and it will also convert API requests and responses so you wouldn’t see the same as seen by the actual daemon when inspecting a container. But when you use a Linux style absolute path, it will still mount it from where the daemon is running.

It was probably not visible from the docker-desktop-data distribution, because

  • that was for the volumes and the daemon was running somewhere in the “docker-desktop” distribution.
  • Recent Docker Desktop has no data distribution, but the “docker-desktop” distribution is special too. And was special back then as well. The docker daemon is running in a container separated from other processes. Listing those processes was made harder so I haven’t tried for a while, but when the Docker daemon is also running in a container, the /home folder is basically the home inside that container, not in the “docker-destkop” distribution. When you mount /var/run/docker.sock it is mounted from that container too. I’m not up to date with the current architecture though

There is no need for SMB. As far as I remember, the WSL distro filesystem is shared with the host using the Plan9 protocol and WSL2 itself is a virtual machine where the distributions are containers. Sharing folders between containers does not require a network filesystem just mounting the container’s filesystem from the VM to the other containers. So if you have files in a WSL distribution, and you enable WSL integration so you can run the docker client in that distribution, mounting a folder from that distribution to the distribution of Docker Desktop should be as efficient as mounting folders from a Linux host into a container running directly on that host, but mounting files from the Windows host requires more and even the filesystem is different.