如何将 Docker 卷挂载到 WSL2? [英] How would I mount a Docker volume to WSL2?

查看:67
本文介绍了如何将 Docker 卷挂载到 WSL2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 Windows 安装了 Docker,然后改用 WSL 2 Linux 容器(没有任何 Linux 知识).

I installed Docker for Windows and switched to using WSL 2 Linux containers (without having any Linux knowledge whatsoever).

当我运行容器并将 Docker 卷安装到 Windows 文件夹时,我收到一条警告消息,告诉我我应该将 Docker 卷安装到 WSL 2 发行版.

When I run a container, mounting a Docker volume to a Windows folder, I get a warning message telling me that I should rather mount the Docker volume to the WSL 2 distribution.

我该怎么做?

当我在 PowerShell 中执行 Docker 时,我似乎无法访问 WSL 2 文件系统.

When I execute Docker in PowerShell, I don't seem to have access to the WSL 2 file system.

推荐答案

如果您真的想将文件夹用作卷,您可以使用 \\wsl$ 将 docker(在 PowerShell 下)指向 WSL 文件系统\ 伪共享.例如:

If you really want to use a folder as a volume, you can point docker (under PowerShell) to the WSL filesystem using the \\wsl$\<distroname> pseudo-share. E.g.:

docker run -ti --volume "\\wsl$\Ubuntu\var\docker\volumes\alpine_persistent_data:/data" --rm alpine

给定一个名为Ubuntu"的发行版(最常见的默认 WSL 安装),它将创建和使用 \var\docker\volumes\alpine_persistent_data 作为卷.

Given a distribution named "Ubuntu" (the most common default WSL install), that will create and use \var\docker\volumes\alpine_persistent_data as a volume.

也就是说,我建议为此使用 Docker 管理的卷而不是目录.请参阅 Docker volumes 文档.类似的东西:

That said, I'd recommend using Docker managed volumes rather than directories for this. See the Docker volumes doc. Something like:

# Optional , since the volume will be created on first use anyway
docker volume create alpine_persistent_data

docker run -ti --volume alpine_persistent_data:/data --rm alpine

# or
docker run -ti --mount source=alpine-persistent-data,target=/data --rm alpine

# Cleanup
docker volume rm alpine-persistent-data

对于 Windows 版 Docker 桌面,卷将存储在您使用 wsl -l -v 命令看到的 docker-desktop-data WSL 实例中.这是一个黑匣子"无法启动的 WSL 实例;它只是由 Docker 桌面后端用于存储图像和卷(可能还有其他 Docker 工件).

With Docker Desktop for Windows, the volumes will be stored in the docker-desktop-data WSL instance that you see with the wsl -l -v command. This is a "black box" WSL instance which cannot be launched; it is simply used by the Docker Desktop backend for storage of images and volumes (and likely other Docker artifacts).

可以检查此 WSL 实例的内容.有关详细信息,请参阅此答案.

You can inspect the contents of this WSL instance. See this answer for details.

这篇关于如何将 Docker 卷挂载到 WSL2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆