简体   繁体   English

npm + wsl + docker 和 volume mounts 有什么解决方案?

[英]What solutions with npm + wsl + docker and volume mounts?

I would like to work on a web project relying on Docker and NPM (This is actually a LAMP server).我想从事一个依赖 Docker 和 NPM 的 Web 项目(这实际上是一个 LAMP 服务器)。 I am running on WSL with the Docker daemon running on Windows.我在 WSL 上运行,而 Docker 守护进程在 Windows 上运行。

Here are my constraints:这是我的限制:

  • To mount volumes with Docker I must locate my project on the Windows filesystem e.g.要使用 Docker 挂载卷,我必须在 Windows 文件系统上找到我的项目,例如/mnt/c/...
  • To use npm which use soft links, I must locate my project on the WSL filesystem e.g.要使用使用软链接的npm ,我必须在 WSL 文件系统上找到我的项目,例如/srv/...

Obviously I cannot meet these two requirements.显然我不能满足这两个要求。

For instance if I do npm install from WSL on a folder located on the Windows filesystem I get plenty of errors such as:例如,如果我在 Windows 文件系统上的一个文件夹上从 WSL 安装 npm,我会收到很多错误,例如:

npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',

Or, simply the modules don't work webpack not found或者,只是模块不工作webpack not found

If I move my project on the WSL side, Docker is not able to mount my project on my target container...如果我将我的项目移到 WSL 端,Docker 将无法将我的项目挂载到我的目标容器上......

What alternative can I use?我可以使用什么替代方案?

When using WSL, Docker for Windows expects you to supply your volume paths in a format that matches this: /c/Users/username/dev/myapp .使用 WSL 时,适用于 Windows 的 Docker 希望您以与此匹配的格式提供您的卷路径: /c/Users/username/dev/myapp

But WSL uses the /mnt/c/Users/username/dev/myapp format.但是 WSL 使用/mnt/c/Users/username/dev/myapp格式。

You have to configure WSL to mount at / instead of /mnt .您必须将 WSL 配置为挂载在/而不是/mnt

Create and modify the new WSL configuration file:创建和修改新的 WSL 配置文件:


    sudo nano /etc/wsl.conf

    # Now make it look like this and save the file when you're done:

    [automount]
    root = /
    options = "metadata"

We need to set root = / because this will make your drives mounted at /c or /e instead of /mnt/c or /mnt/e.我们需要设置 root = / 因为这将使您的驱动器安装在 /c 或 /e 而不是 /mnt/c 或 /mnt/e。

The options = "metadata" line is not necessary but it will fix folder and file permissions on WSL mounts so everything isn't 777 all the time within the WSL mounts. options = "metadata" 行不是必需的,但它会修复 WSL 挂载上的文件夹和文件权限,因此 WSL 挂载中的所有内容都不会一直为 777。 I highly recommend you do this!我强烈建议你这样做!

Once you make those changes, sign out and sign back in to Windows to ensure the changes take effect进行这些更改后,注销并重新登录 Windows 以确保更改生效

source: https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly来源: https ://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM