Slav
Slav

Reputation: 27495

WSL1, Docker Desktop, volume mounts are always empty

I followed this setup completely. Running WSL1 with Docker Desktop on Windows 10. I am not interested in WSL2 at this point. I don't have Insider Windows.

Now, I am trying to start a container with a volume, so that the container's files are copied into the volume. According to official docs:

Populate a volume using a container

If you start a container which creates a new volume, as above, and the container has files or directories in the directory to be mounted (such as /app/ above), the directory’s contents are copied into the volume.

So it should be possible, but I must be missing something really basic here, cause it just doesn't work.

I've tried -v vol-name:/path/on/container -> this creates a named volume... somewhere. No clue where, nor how to view it. Doing volume inspect vol-name shows a path that doesn't exist, neither in WSL nor in Docker Host (Windows). I even tried mounting the MobyVM and it isn't there either.

I've tried -v /c/full/path:/path/on/container -> this creates a bind-type mount. It's empty (by design). If it put files under /c/full/path, I will see them in container under /path/on/container, but that's not what I need. I need to populate the volume with contents from container. From what I understand from documents, I need a volume-type mount, not bind-type mount. In this case the -v options forces bind-type

I've tried --mount type=volume,source=/c/full/path,destination=/path/on/container -> This results in this error: docker: Error response from daemon: create /c/full/path: "/c/full/path" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path. The path separator is not allowed...

I've read something about special characters in passwords being an issue and reset my password I've read about the /c/full/path needing full access permission, and gave "everyone" full access

Please help

Upvotes: 0

Views: 3612

Answers (2)

Damo
Damo

Reputation: 6443

I think that I have the same setup as you: windows 10, WSL1, docker desktop 2.2.0.0 (42247)

➜  uname -a
Linux LAPTOP1001 4.4.0-17134-Microsoft #1130-Microsoft Thu Nov 07 15:21:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
➜  lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic

I have managed to perform the same operations as you, i did this in the following manner using alpine:

➜  docker volume create my-vol
my-vol
➜  docker volume ls
DRIVER              VOLUME NAME
local               my-vol
➜  docker volume inspect my-vol
[
    {
        "CreatedAt": "2020-02-07T11:06:15Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
        "Name": "my-vol",
        "Options": {},
        "Scope": "local"
    }
]
➜  docker run -it --name volTest --mount source=my-vol,target=/my-vol alpine
/ # cd my-vol
/my-vol # ls
/my-vol # exit
➜  docker run -it --name volTest2 --mount source=my-vol,target=/usr/bin alpine
/ # cd /usr/bin/
/usr/bin # ls -al
total 228
drwxr-xr-x    2 root     root          4096 Feb  7 11:16 .
drwxr-xr-x    7 root     root          4096 Jan 16 21:52 ..
lrwxrwxrwx    1 root     root            12 Jan 16 21:52 [ -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jan 16 21:52 [[ -> /bin/busybox
...
...
/usr/bin # exit 
➜  docker container start volTest
volTest
➜  docker container attach volTest
/ # cd /my-vol/
/my-vol # ls
total 228
drwxr-xr-x    2 root     root          4096 Feb  7 11:16 .
drwxr-xr-x    7 root     root          4096 Jan 16 21:52 ..
lrwxrwxrwx    1 root     root            12 Jan 16 21:52 [ -> /bin/busybox
lrwxrwxrwx    1 root     root            12 Jan 16 21:52 [[ -> /bin/busybox
...
...
/my-vol # exit
➜ 
  1. Firstly I create a volume my-vol and inspect it
  2. Secondly my-vol is attached to the root of the container myTest, when you look inside the volume it is empty
  3. Then attach my-vol to another container called myTest2 but in the folder /usr/bin which as you can see contains lots of files.
  4. Now restarting myTest you can see the volume has been populated with the files from /usr/bin from the container myTest2

Can you replicate my results on your system? Cheers

Upvotes: 0

Damo
Damo

Reputation: 6443

Let me summarize what i think your setup is then i will try and give a solution.

  • You are running docker desktop for the docker engine
  • You are connecting to docker desktop via the docker cli installed on WSL
  • You are trying to share a windows folder with a running container
  • You have enabled sharing of your C drive in the settings of docker desktop

I think that you are linking to the wrong path, the path you give needs to be recognized by docker desktop which remember is running in windows, therefore the path needs to be in the format c:/full/path.

So try the following to test if you have everything setup correctly

➜  cd /mnt/c
➜  mkdir -p full/path
➜  cd full/path
➜  pwd
/mnt/c/full/path
➜  docker image pull alpine
Using default tag: latest
latest: Pulling from library/alpine
Digest: sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d
Status: Image is up to date for alpine:latest
docker.io/library/alpine:latest
➜  date > foobar.txt
➜  cat foobar.txt
Thu Feb  6 17:49:31 STD 2020
➜  docker run --rm -v c:/full/path:/full/path alpine cat /full/path/foobar.txt
Thu Feb  6 17:49:31 STD 2020
➜  

to finish off you can use wslpath along with pwd to get the current dir in a form that docker desktop can use.

docker run --rm -v $(wslpath -w $(pwd)):/full/path alpine ls /full/path/

Hope this helps

Upvotes: 3

Related Questions