Table of Content

Assume you work on windows on daily base, want to get benefit from docker container using docker toolbox for test and dev purpose.

run docker container on windows

There are two options to run docker container on windows:

  • run docker container directly from local window shell, like msys or cygwin
  • run docker container in side docker host in virtualbox vm

In fact when you run docker locally, container still running on docker host vm, difference is you can easily integrated docker with native windows tools, msys/cygwin bash is in same position as OSX linux, docker toolbox is just for this purpose:

docker-machine - create and manage docker host in vm
docker-compose - define and run multi-containers in one docker host vm based on compose file yml
docker client - run docker container directly from win local

data volume mapping and mounting

data volume mapping and mounting is most confuse part when run docker container on windows, there are 2 stages map/mount involved here:

  • virtualbox build in share folder feature: it mount win local folder to docker host vm
  • docker client mapping docker host vm local folder to container

these are key facts for 2 stages mapping:
1) virtualbox sharing from win host to docker host, default share folder:
c/Users to C:\Users
2) the sharing path should be exactly same on win host and docker host

let’s have a look examples.

use case 1

mapping win local work folder under C:\Users to docker container

in msys bash

$ cd /c/Users/USERNAME/mount/
there is README.md there,
$ cat README.md
test docker volume , mount from msys local folder to docker container

we want to mount win local folder to container /code, and verify mounting:

$ docker run -it -v $PWD:/code -w /code busybox ash -c 'pwd;ls -ltr;cat README.md'
C:\oldhorse\portableapps\msys64\usr\local\bin\docker.exe: Error response from daemon: invalid bind mount spec "C:\\Users\\USERNAME\\mount;C:\\oldhorse\\portableapps\\msys64\\code": invalid mode: \oldhorse\portableapps\msys64\code.
See 'C:\oldhorse\portableapps\msys64\usr\local\bin\docker.exe run --help'.

This error is from msys, which converted unix path to DOS path then send to docker, can solve by adding back slash as below:

$ echo $PWD
/c/Users/USERNAME/mount

$ docker run -it -v /$PWD:/code -w //code busybox ash -c 'pwd;ls -ltr;cat README.md'
/code
total 1
-rwxrwxrwx    1 1000     staff           71 Sep 10 18:29 README.md
test docker volume , mount from msys local folder to docker container

you can verify mapping full path mounted in docker host vm, which is exactly same as local folder in msys.

$ docker-machine ssh msysdev
docker@msysdev:~$ df -h |grep Users
c/Users                 439.8G    209.9G    229.8G  48% /c/Users
docker@msysdev:~$ ll /c/Users/USERNAME/mount/
total 1
-rwxrwxrwx    1 docker   staff           71 Sep 10 18:29 README.md

use case 2

mapping win local work folder not under C:\Users to docker container
for example /home/USERNAME/test in msys

first of all, need to add share folder in virtualbox manually:
c/oldhorse to C:\oldhorse

then restart docker machine to make share folder working,
$ docker-machine restart msysdev

verified inside docker host vm, local folder mounted properly,

docker@msysdev:~$ df -h|grep oldhorse
c/oldhorse              439.8G    209.9G    229.8G  48% /c/oldhorse
docker@msysdev:~$ ll /c/oldhorse/portableapps/msys64/home/USERNAME/test
total 1
-rwxrwxrwx    1 docker   staff          135 Sep 10 19:08 README.md
docker@msysdev:~$ cat /c/oldhorse/portableapps/msys64/home/USERNAME/test/README.md
docker volume test:
- virtualbox: mount from msys local folder to docker host
- docker volume: mapping from docker host to container

however it is not working as expected:

$ docker run -it -v /$PWD:/code -w //code busybox ash -c 'pwd;ls -ltr;cat README.md'
/code
total 0
cat: can't open 'README.md': No such file or directory

the issue is on $PWD, which is short path in msys, when docker mount this path from docker host to container, it is not existing.

$ echo $PWD
/home/USERNAME/test

so we have to use full path in this case,

$ docker run -it -v //c/oldhorse/portableapps/msys64/home/USERNAME/test:/code -w //code busybox ash -c 'pwd;ls -ltr;cat README.md'
/code
total 1
-rwxrwxrwx    1 1000     staff          135 Sep 10 19:08 README.md
docker volume test:
- virtualbox: mount from msys local folder to docker host
- docker volume: mapping from docker host to container

use case 3

Of course you always can do docker volume mapping inside docker host, just make sure sharing local folder to docker host before mapping them from docker host to container.

$ docker-machine ssh msysdev
docker@msysdev:~$ ll /c/oldhorse/portableapps/msys64/home/USERNAME/test
total 1
-rwxrwxrwx    1 docker   staff          135 Sep 10 19:08 README.md
docker@msysdev:~$ df -h|grep oldhorse
c/oldhorse              439.8G    209.9G    229.8G  48% /c/oldhorse
docker@msysdev:~$ ll /c/oldhorse/portableapps/msys64/home/USERNAME/test
total 1
-rwxrwxrwx    1 docker   staff          135 Sep 10 19:08 README.md
docker@msysdev:~$ docker run -it -v /c/oldhorse/portableapps/msys64/home/USERNAME/test:/code -w /code busybox ash -c 'pwd;ls -ltr;cat README.md'
/code
total 1
-rwxrwxrwx    1 1000     staff          135 Sep 10 19:08 README.md
docker volume test:
- virtualbox: mount from msys local folder to docker host
- docker volume: mapping from docker host to container

Conclusion

  • docker volume can be mapped from win locally or inside docker host
  • make sure share folder setup in virtualbox before docker volume mapping
  • the unix format full path name should be same in msys and docker host