Windows docker 卷的 WSL2 安装 VHDX 虚拟磁盘问题

WSL2 mount VHDX virtual disk issue with Windows docker Volumes

在 Windows 10 Insider Preview (prerelease.201207-1443) 我已经使用 WSL2 成功安装了一个 vhdx 文件。

PS C:\Users> wmic diskdrive list brief
Caption                     DeviceID            Model                       Partitions  Size
..
Microsoft virtuális lemez   \.\PHYSICALDRIVE2  Microsoft virtuális lemez   1           5362882560

PS C:\Users> wsl --mount \.\PHYSICALDRIVE2 --bare

在 Ubuntu 20.20 中看起来不错,我可以挂载并格式化 (ext4) 虚拟磁盘。

/dev/sde1       4.9G   20M  4.6G   1% /mnt/docker/vol/pg_disk_1

能用,读,写都没问题。性能不错。

问题:我想在为应用程序数据创建 docker 容器时使用此磁盘,它在容器内不可见。

docker run -d -it --name devtest --mount type=bind,source=/mnt/docker/vol/pg_disk_1/nginx,target=/app nginx:latest

docker 检查显示已安装的卷绑定:

        "Mounts": [
        {
            "Type": "bind",
            "Source": "/mnt/docker/vol/pg_disk_1/nginx",
            "Destination": "/app",
            "Mode": "",
            "RW": true,
            "Propagation": "rprivate"
        }
    ],

当我在主机上创建文件时,例如。 example_host.html

User@Pince:/mnt/docker/vol/pg_disk_1/nginx$ ls -l
total 0
-rw-r--r-- 1 r858 r858 0 Jan  3 11:30 example_host.html

检查容器内部时不可见

root@078e4d7007a8:/app# ls -la
total 8
drwxr-xr-x 2 root root 4096 Jan  3 10:32 .
drwxr-xr-x 1 root root 4096 Jan  3 10:15 ..
root@078e4d7007a8:/app# pwd
/app

如果我在容器内创建一个文件,它会在重新创建容器时保持不变,但不会在 Linux 安装的磁盘中保持不变。我在这里找到它:

\wsl$\docker-desktop\mnt\host\wsl\docker-desktop-bind-mounts\Ubuntu-20.04\...

问题:这是正常的,我错过了一些东西,或者在这个开发阶段还没有准备好。你知道有人知道如何使用 vhdx 挂载磁盘存储 Windows 上的 docker 应用程序数据吗?

问题是 /dev/sde1 是由 wsl 创建和管理的,因此 Windows 程序(例如 Docker Desktop)无法(轻松?)访问它。

一种解决方法是将 \.\PHYSICALDRIVE2 安装为驱动器 D: 并安装为:

docker run -v 'D:\data:/data' ...

这样,您可以在 Windows 和 wsl 上访问它。