Sebastian Nemeth
Sebastian Nemeth

Reputation: 6175

Docker on WSL won't bind mount $HOME

I have the strangest situation using Docker on WSL (Windows Subsystem for Linux, Ubuntu 16.04). I'm trying to bind mount /home/username (or just $HOME for convenience) as a volume in a container, and instead of finding the content of my home directory in the container, I get some other volume entirely.

What's stranger is that this 'other volume' persists from one container to the other, whenever I try to bind mount $HOME or /home/username. If I touch a new file, it appears in all other containers I mount $HOME into. All other bind mounts to any other directory work correctly.

E.g. these all share the same mystery folder:

docker run -it --rm -v /home/username:/test alpine sh
docker run -it --rm -v $HOME:/test alpine sh
docker run -it --rm -v $HOME:/test -v $HOME:/test2 alpine sh

When I do a docker volume ls there's no volume called /home/username, so that rules out accidentally having a docker-hosted volume with the same name.

What is this mysterious volume I'm mounting, and why is docker not mounting my $HOME directory correctly?

Upvotes: 5

Views: 1409

Answers (2)

Damo
Damo

Reputation: 6443

There is no need to change the mound point as suggested by @rfay. Rather if you use a little command foo you can use pwd and sed to fix the value for you.

docker run -it -v $(pwd | sed 's/^\/mnt//'):/var/myfolder -w "/var/myfolder" centos:7

pwd will return the current working folder, usually in the format '/mnt/c/code/myfolder'. Piping this to sed and replacing '/mnt' with nothing will leave you with a path such as '/c/code/myfolder' which is the desired path for docker for windows. you need to wrap the whole thing in $() to cause it to be executed in place.

I find this works really well.

Upvotes: 0

rfay
rfay

Reputation: 12905

I used the instructions in https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly#ensure-volume-mounts-work to set up everything.

Then I had to explicitly export HOME=/c/Users/rfay so that the Docker daemon on Windows could access it. But that worked. The basic magic is that your pathing in WSL has to be something that the Docker daemon can translate in native Windows.

Upvotes: 1

Related Questions