首页 > 解决方案 > Windows 10 Linux 子系统中的 Dockerfile 在当前目录中找不到文件

问题描述

我有Dockerfile最后一行:

CMD ["./watch.sh"]

哪个只是运行npm run watch. 在 Linux 中,一切正常。但是在 Windows 10 Subsytem for Linux (WSL) 中,docker 会抛出一个错误,说它找不到文件。在另一个Dockerfile运行python <SOMEFILE.PY>它还说它找不到文件。

我已经将这些设置在/etc/wsl.conf

[automount]
root = /
options = "metadata"

我在我的用户名进程中运行 docker,并且已经将我的名字放在了 docker 组中。所以,我不把它作为sudo. 文件权限也在我的帐户中。

编辑:我只是意识到,似乎我需要将docker客户端运行为sudo. 在 Bash WSL 中,似乎找不到驻留在 Windows 中的 docker 引擎,通过 tcp://localhost:2375 访问

$ sudo usermod -aG docker root
$ sudo docker build -t build-minimal .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

当我在没有 sudo 的情况下运行 docker 时,它可以工作。

标签: dockerwindows-subsystem-for-linux

解决方案


找到了!答案是文件权限。

我使用 WSL 的意图是拥有完全兼容的 Linux 环境,但仍然能够使用 Windows 工作。因此,Linux 和 Windows 之间的文件共享必须正确完成。

我以前的错误只是从 Linux 到我的 Windows 文件(即 Python Flask 应用程序)的符号链接。这很糟糕,因为文件权限将无法正确存储/读取。

解决方案:

  • 指向代码/数据目录的符号链接位于 Windows 文件夹中
  • 如果是 git 存储库,请git clone从 WSL 框中执行
  • 这将使文件权限正确保留,您仍然可以从 WSL/Windows 中完美地访问它们

推荐阅读