Docker Bind Mounts in WSL do not show files

依然范特西╮ 提交于 2020-06-17 09:54:39

问题


Im trying to use the docker client from inside WSL, connecting to the docker engine on Windows. Ive exposed the docker engine on Windows on port 2375, and after setting the DOCKER_HOST environment variable in WSL, I can verify this works by running docker ps.

The problem comes when i attempt to mount directories into docker containers from WSL. For example:

  • I create a directory and file inside my home folder on WSL (mkdir ~/dockertest && touch ~/dockertest/example.txt)
  • ls ~/dockertest shows my file has been created
  • I now start a docker container, mounting my docker test folder (docker run -it --rm -v ~/dockertest:/data alpine ls /data)
  • I would expect to see 'example.txt' in the docker container, but this does not seem to be happening.

Any ideas what I might be missing?


回答1:


There are great instructions for Docker setup in WSL at https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly#ensure-volume-mounts-work - solved most of my problems. The biggest trick for me was with bind-mounted directories; you have to use a path in WSL that the Docker daemon will be able to translate, for example /c/Users/rfay/myproject.




回答2:


Anyone stumbling here over this issue follow this: Docker Desktop WSL2 Backend and make sure you are running the version 2 of the WSL in PowerShell:

> wsl -l -v
  NAME                   STATE           VERSION
* docker-desktop         Running         2
  Ubuntu                 Running         2
  docker-desktop-data    Running         2

If your Ubuntu VERSION doesn't say 2, you need to update it according to the guide above.

After that, you will be able to mount your Linux directory to the Docker Container directly like:

docker run -v ~/my-project:/sources <my-image>



回答3:


I don't think you need to change the mound point as the link suggests. Rather if you use pwd and sed in combination you should get the effect you need.

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

pwd returns the working folder in the format '/mnt/c/code/folder'. Pipe this to sed and replace '/mnt' with empty string will leave you with a path such as '/c/code/folder' which is correct for docker for windows.



来源:https://stackoverflow.com/questions/51627619/docker-bind-mounts-in-wsl-do-not-show-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!