You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Volume挂载指定目录失败,如何实现自定义路径存储?

问题描述

我编写了如下Docker Compose文件,想要创建一个Volume并将所有Volume数据挂载到指定目录D:\test。

我的Docker Compose代码

version: '3'

volumes: 
  test:
    driver: local
    driver_opts: 
      type: 'none'
      o: 'bind'
      device: 'D:\test'

services:
  mongodb:
    image: mongo
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - test:/data/db
  
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
    volumes:
      - test:/etc/mongo-express

运行错误信息

执行docker-compose -f mongo.yaml up -d时出现错误,提示特殊设备D:\test不存在:

D:\Docker-compose>docker-compose -f mongo.yaml up -d
[+] Running 19/19
 ✔ mongo-express 7 layers [⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                        18.5s
   ✔ b0e1773ee89e Download complete                                                                                0.0s
   ✔ 0dc151cc3eb8 Download complete                                                                               14.0s
   ✔ 9398808236ff Download complete                                                                                1.9s
   ✔ 1de49a94a524 Download complete                                                                                9.3s
   ✔ ee8c69d204ca Download complete                                                                                0.6s
   ✔ 844e0ac2d780 Download complete                                                                                0.0s
   ✔ 81bee2a0ce57 Download complete                                                                                0.0s
 ✔ mongodb 10 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                          34.9s
   ✔ e062d62b861e Download complete                                                                                0.0s
   ✔ 54a7480baa9d Download complete                                                                                0.0s
   ✔ 43f89b94cd7d Download complete                                                                               14.3s
   ✔ 7f9301fbd7df Download complete                                                                                1.8s
   ✔ 40d046ff8fd3 Download complete                                                                                0.0s
   ✔ 5e4470f2e90f Download complete                                                                                0.4s
   ✔ 74c3fb6d33e0 Download complete                                                                               30.3s
   ✔ 72919e34fde8 Download complete                                                                                0.0s
   ✔ adfd395e3ac5 Download complete                                                                                0.0s
   ✔ 021b676f1558 Download complete                                                                                0.0s
[+] Running 2/4
 ✔ Network docker-compose_default            Created                                                               0.1s
 ✔ Volume "docker-compose_test"              Created                                                               0.0s
 - Container docker-compose-mongo-express-1  Starting                                                             10.3s
 - Container docker-compose-mongodb-1        Starting                                                             10.3s
Error response from daemon: mounting volume docker-compose_test for container da061e8124ac646ea67d4d2bd563db7836a2ddab3a7e44c6e72415e08ff7bff2: mount: /var/lib/containers/storage/volumes/docker-compose_test/_data: special device D:\test does not exist.
       dmesg(1) may have more information after failed mount system call.

Volume检查结果

通过docker volume inspect查看,Volume已创建但无法正常挂载:

volume ls
DRIVER    VOLUME NAME
local     693a25c1c5ba909f6f56ad27587ddd7bb9ebf0220ab54f9c25dc86bd8acedd3c
local     a166c931596773410b2db1cc6e6e19d45dd0ae7e43de939fd5ace793e161aff3
local     buildx_buildkit_default_state
local     docker-compose_test

D:\Docker-compose>docker inspect docker-compose_test
[
    {
        "CreatedAt": "2023-11-27T19:55:06+05:30",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "docker-compose",
            "com.docker.compose.version": "2.17.3",
            "com.docker.compose.volume": "test"
        },
        "Mountpoint": "/var/lib/containers/storage/volumes/docker-compose_test/_data",
        "Name": "docker-compose_test",
        "Options": {
            "device": "D:\\test",
            "o": "bind",
            "type": "none"
        },
        "Scope": "local"
    }
]

请问如何将所有Volume数据存储到我指定的目录?


解决方案

问题根源

Windows环境下Docker daemon运行在WSL2的Linux虚拟机中,无法直接识别Windows盘符格式的路径D:\test,必须使用WSL2挂载后的Linux风格路径。

修复步骤

1. 确认WSL2中的对应路径

Windows的D盘在WSL2中默认挂载路径为/mnt/d,因此D:\test对应的Linux路径是/mnt/d/test。

2. 修改Docker Compose文件

将volume配置中的device参数替换为WSL2识别的路径,同时确保目标目录已在Windows中创建:

version: '3'

volumes: 
  test:
    driver: local
    driver_opts: 
      type: 'none'
      o: 'bind'
      device: '/mnt/d/test'  # 替换为WSL2格式路径

services:
  mongodb:
    image: mongo
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - test:/data/db
  
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
      - ME_CONFIG_MONGODB_SERVER=mongodb
    volumes:
      - test:/etc/mongo-express

3. 清理旧Volume并重新部署

先删除已创建的异常Volume:

docker volume rm docker-compose_test

再重新启动服务:

docker-compose -f mongo.yaml up -d

额外优化建议

不要让MongoDB和Mongo-Express共享同一个Volume挂载到不同容器目录,这会导致两个服务的文件互相干扰。建议为每个服务单独配置绑定挂载:

services:
  mongodb:
    # ...其他配置
    volumes:
      - /mnt/d/test/mongo:/data/db  # 直接绑定到子目录
    
    mongo-express:
      # ...其他配置
      volumes:
        - /mnt/d/test/mongo-express:/etc/mongo-express

内容的提问来源于stack exchange,提问作者Akshay Durgade

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 13:50:10