使用Docker for Windows将Windows驱动器挂载到Linux容器中

11

我正在使用 Windows 的 Docker 桌面版,我想找到由 Docker 在 Linux 容器中创建的卷的位置?

是否有人能够执行我尝试实现的卷挂载操作?

2个回答

30

如果您只是想将Windows路径挂载到基于Linux的容器中,这里提供一个使用基本的docker run命令和Docker Compose示例的例子:

docker run -d --name qbittorrent -v '/mnt/f/Fetched Media/Unsorted:/downloads' -v '/mnt/f/Fetched Media/Blackhole:/blackhole' linuxserver/qbittorrent

这个例子将Windows主机上的f:\Fetched Media\Unsortedf:\Fetched Media\Blackhole文件夹共享到容器中;在Linux容器中,您会看到这些Windows文件夹中的文件分别显示在冒号右侧的各自Linux路径中。
f:\Fetched Media\Unsorted文件夹将出现在Linux容器中的/downloads文件夹中。
*首先,请确保在GUI中的Docker Desktop设置区域共享了这些Windows文件夹。
WSL(2)更新
您不需要特别共享Windows文件夹路径;这仅在不使用WSL时才需要。
更新: 由于这似乎是一个常见的问题,因此我也想包括上面示例的Docker Compose版本,以求全(包括如何将路径设置为读写(rw)或只读(ro))。
qbittorrent:
  image: 'linuxserver/qbittorrent:latest'
  volumes:
    - '/mnt/f/Fetched Media/Unsorted:/downloads:rw'
    - '/mnt/f/Fetched Media/Blackhole:/blackhole:rw'
    - '/mnt/e/Logs/qbittorrent:/config/logs:rw'
    - '/opt/some-local-folder/you-want/read-only:/some-folder-inside-container:ro'

3
我在互联网上到处搜索了这个。非常感谢。真的很惊讶它不在Docker文档中。 - Iwnnay
能否使用共享网络驱动器来实现这个?我尝试过,但映射的文件夹为空。 - liam
1
@liam 这是一个有难度的问题!哈哈。请看这里:https://forums.docker.com/t/mounted-windows-share-doesnt-show-contents-within-docker-container/100343 - J. Scott Elblein
1
从干净的 Windows 11 安装中安装 Docker Desktop for Windows 和 WSL 2,没有像 Ubuntu 这样的发行版,它无法识别 /mnt。直接从驱动器字母开始工作:/f/Fetched Media/Unsorted。 - RotBot

9
我不确定为什么需要这样做,但如果有其他人遇到这个问题:我必须将整个卷声明用双引号括起来,如下所示。
docker run -d --name qbittorrent -v "/mnt/f/Fetched Media/Unsorted:/downloads" -v "/mnt/f/Fetched Media/Blackhole:/blackhole" linuxserver/qbittorrent

否则,J. Scott Elblein的答案完美地解决了问题!

1
你是正确的;有时候将最后一个引号延伸到末尾可以帮助整个字符串被引用。如果路径中有奇怪的字符,比如点,对于隐藏文件夹的情况,我通常发现这是必要的。 - J. Scott Elblein
注意:为避免任何可能的混淆,我刚刚更新了我的答案以反映您的答案。 :) - J. Scott Elblein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接