Windows 10 Linux 子系统中的 Dockerfile 在当前目录中找不到文件

Dockerfile in Windows 10 Linux Subsystem can't find file in current directory

我有一个 Dockerfile 最后一行:

CMD ["./watch.sh"]

这只是 运行宁 npm run watch。 在 Linux 中,一切正常。 但是在 Windows 10 Subsytem for Linux (WSL) 中,docker 会抛出一个错误,说找不到文件。在另一个 Dockerfile 中 运行s 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?

当我 运行 docker 没有 sudo 时,它工作。

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

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

我之前的错误只是从 Linux 到我的 Windows 文件的符号链接(即 Python Flask 应用程序)。这很糟糕,因为文件权限不会 store/read 正确。

解决方案:

  • 指向您 code/data 目录的符号链接驻留在 Windows 文件夹中
  • 如果它是 git 存储库,请从 WSL 框
  • 中执行 git clone
  • 这将正确保留文件权限,并且您仍然可以从 WSL/Windows 中完美地访问它们