Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Named Volume with targeting windows local folder

In docker-compose file I want to create a named volume which will target local drive for test purposes. For production we will use NFS.

I created the compose file as following,

version: '3.3'
services:

  test:
    build: .
    volumes:
      - type: volume
        source: data_volume
        target: /data
    networks:
      - network

volumes:
  data_volume:
    driver: local
    driver_opts:
      o: bind
      type: none
      device: c:/data

networks:
  network:
    driver: overlay
    attachable: true

When I run the docker-compose up, I got the following error,

for test_test_1  Cannot create container for service test: failed to mount local volume: 
mount c:/data:/var/lib/docker/volumes/test_data_volume/_data, flags: 0x1000: no such file 
or directory

Even with errors, it still creates the named volume. So when I inspect it,

{
    "CreatedAt": "2019-10-07T09:10:14Z",
    "Driver": "local",
    "Labels": {
        "com.docker.compose.project": "test",
        "com.docker.compose.version": "1.24.1",
        "com.docker.compose.volume": "data_volume"
    },
    "Mountpoint": "/var/lib/docker/volumes/test_data_volume/_data",
    "Name": "test_data_volume",
    "Options": {
        "device": "c:/data",
        "o": "bind",
        "type": "none"
    },
    "Scope": "local"
}

I'm still not sure why the Mountpoint is targeting that location. I know I can achieve this without named volume (which I already did), but for future in the project we definitely need named volume.

Any suggestion how to achieve this?

like image 578
Serdar Avatar asked Jan 01 '23 14:01

Serdar


1 Answers

Same here. Using Docker Desktop for Windows, I tried to mount the local path E:\Project\MyWebsite\code to the named volume but failed. Here's how I sorted this out.

First, I changed the path to ".":

volumes:
    website:
        driver: local
        driver_opts:
            type: none
            device: "."
            o: bind

This time docker-compose up ran successfully, so I logged into the shell and checked how the mounted directory looks like:

bash-5.0# ls -l
total 62
lrwxrwxrwx    1 root     root            11 Oct  1 15:15 E -> /host_mnt/e
drwxr-xr-x    2 root     root         14336 Sep 11 15:27 bin
drwxr-xr-x    4 root     root          2048 Apr 19  2017 dev
lrwxrwxrwx    1 root     root            11 Oct  1 15:15 e -> /host_mnt/e
drwxr-xr-x    1 root     root           180 Sep 30 11:53 etc
drwxr-xr-x    2 root     root          2048 Sep 11 15:27 home
drw-r--r--    4 root     root            80 Oct  8 22:52 host_mnt
drwxr-xr-x    1 root     root            60 Sep 30 11:53 lib
drwxr-xr-x    5 root     root          2048 Sep 11 15:27 media
...
drwxrwxrwt    1 root     root            40 Oct 11 19:37 tmp
drwxr-xr-x    1 root     root            80 Sep 11 15:27 usr
drwxr-xr-x   13 root     root          2048 Sep 11 15:27 var

Obviously not a Windows volume, probably some Linux VM created by Docker. But the paths /host_mnt/e and /host_mnt/E seem indicative, so I tried changing the docker-compose definition to:

volumes:
    website:
        driver: local
        driver_opts:
            type: none
            device: "/host_mnt/e/Project/MyWebsite/code"
            o: bind

And it worked! Looks like named volume doesn't work the same as the ordinal way for Windows.

This /host_mnt/e probably won't exist unless the you've granted access to the drive letter before. But this shouldn't be an issue to you, as you'd tried the ordinal way of mounting a local drive which worked.

like image 173
Edmund Tam Avatar answered Jan 13 '23 13:01

Edmund Tam