Mount Any Windows Folder into Containers on Docker for Windows

In a native Docker environment, you can mount /source in a container host onto /destination in a container by docker run -v /source:/destination and access it from the container.

Well then, how can you mount C:\Source in Windows onto /destination in a container on Docker for Windows? You can't directly mount C:\Source in the VM host into the container, of course.

As the first step, you have to set up C:\Source as a shared folder for a VM when you create it with docker-machine. You can specify the shared folder with the --virtualbox-share-folder option of the VirtualBox deriver as follows.

docker-machine create --driver virtualbox --virtualbox-share-folder=C:\Source:Source

C:\Source:Source specifies C:\Source as a shared folder named Source. The VM automatically mounts it onto /Source in the container host by default.

Then, you have to mount /Source onto /destination in a container by the -v option of the docker command as follows: docker run -v /Source:/destination Finally, you can access C:\Source through /destination as follows.

By the way, usual VM's by VirtualBox mount a shared folder named Source onto /media/sf_Source in the guest OS. The VM created by docker-machine mounts /Source. It is because the VirtualBox driver set MountDir(/media) to / and MountPrefix(sf_) to / as follows in the VM configuration.

For C:\Users

If you don't specify --virtualbox-share-folder, the driver use \?c:\Users:c/Usersas the default value. The VM mounts C:\User onto /c/Users in a container host.

You can just run docker run -v /c/Users:/destination to access C:\User through /destination. You don't need to specify --virtualbox-share-folder if you want to access C:\User from the container.

For VMware Workstation

You can use VMware Workstation as a hypervisor for Docker for Windows. The Installation section in Docker Machine VMware Workstation Driver explains how to do it.

The VMware Workstation driver has no option to specify a shared folder. You have to modify the VM configuration after created. If you specify dev as the VM name, you can find the following setting in %USERPROFILE%/.docker/machine/machines/dev/dev.vmx. It specifies C:\User as a shared folder named Users.

sharedFolder0.hostPath = "C:\Users"
sharedFolder0.guestName = "Users"

You should directly change it to C:\Source or set the same setting by VMware Workstation.

sharedFolder0.hostPath = "C:Source"
sharedFolder0.guestName = "Source"

The VM by VMware Workstation automatically mounts the shared folder named Source onto /mnt/hgfs/Source in the container host. You can access C:\Source through /destination by docker run -v /mnt/hgfs/Source:/destination.

For C:\Users with VMware Workstation

By the way, the VMware driver for docker-machine not only set C:\Users as a shared folder by default, but mount it onto /Users and make a symbolic link /c/Users -> /Users in the container host.

As a result, how to access C:\Users from /destination in a container is just the same as the case of VirtualBox driver, that is, docker run -v /c/Users:/destination.

To achieve this compatibility with VirtualBox, the VMware driver runs the following shell script on a container host when you run docker-machine start.

"C:/Program Files (x86)"/VMware/VMware Workstation/vmrun.exe -gu docker -gp tcuser 
runScriptInGuest C:Usersfujie.dockermachinemachinesdevdev.vmx 
  /bin/sh [ ! -d /Users ]&& sudo mkdir /Users; 
    [ ! -d /c ]&& sudo mkdir -p /c; 
    [ ! -d /c/Users ]&& sudo ln -s /Users /c/Users; 
    [ -f /usr/local/bin/vmhgfs-fuse ]&& 
      sudo /usr/local/bin/vmhgfs-fuse -o allow_other .host:/Users /Users || 
        sudo mount -t vmhgfs .host:/Users /Users

The actual is a one-liner.