Sunday 27 November 2016

Docker Toolbox on Windows: Mount any host directory (other than Users directory in c drive) as a data volume

This post is for you, if you are looking for mounting a host directory (which is not underneath the "C:\Users" folder) as a data volume in docker container using Docker Toolbox on Windows.

Problem Statement:


As per docker documentation --- "If you are using Docker Machine on Mac or Windows, your Docker Engine daemon has only limited access to your OS X or Windows filesystem. Docker Machine tries to auto-share your /Users (OS X) or C:\Users (Windows) directory. So, you can mount files or directories on OS X using".

This means, you can mount anything underneath "c:\Users" directory of host your Windows machine as a data volume in docker container seamlessly. For example, let's say you want to dump some data from "c:\Users\tirthalp\data" directory to "data" directory within Redis container in docker. Well, below just executing command will enable you to do it.

docker run -v /c/Users/tirthalp/.docker/data:/data --name <container-name> <image-name>


But, what is there is a need of mounting other folders (any folder which is not underneath "c:\Users" directory) of your Windows machine as a data volume in docker container? Well, that's not a piece of cake. For example, if you execute below command, the container would not complain. But actually mounting of folder in D drive as a data volume could not happen and data from your host Windows machine would not be accessible in docker container.

docker run -v /d/x-temp/data:/data --name <container-name> <image-name>


Solution:

I could not locate official docker documentation on this. After giving few tries, finally these steps worked --- add a shared folder to Docker Host VM and mount it as a data volume. Although the given steps are pretty clear, I thought to share screenshots of the same as following.

Step 1: Stop Docker Machine


Step 2: Add Shared Folder in Oracle VirtualBox Manager

Open Oracle VM VirtualBox Manager. Settings -> Shared Folder -> Add Share Folder (i.e. Tirthal-LABs).


Step 3: Permanently mount VirtualBox shared folder as a shared-data volume in Docker machine

Start docker machine.


SSH to docker machine, create directory in docker machine (i.e. /home/docker/tirthal-projects) and mount VirtualBox shared folder name (i.e. Tirthal-LABs) with it. This should enable you to access host Windows machine's data in docker machine @ /home/docker/tirthal-projects.


Try restarting docker machine. Now if you check at /home/docker/tirthal-projects, you would not see mounted host machine's data. For permanent mounting, add following two lines at the end of profile --- sudo vi /mnt/sda1/var/lib/boot2docker/profile

mkdir /home/docker/<directory-name>
sudo mount -t vboxsf -o uid=1000,gid=50 <virtual-box-shared-folder-name> /home/docker/<directory-name>


Try restarting docker machine again. Now if you check at /home/docker/tirthal-projects, you would not see mounted host machine's data.


Step 4: Mount a shared-storage volume as a data volume

Now you should be able to mount even other than "c:\Users" directories of host Windows machine in docker container as a data volume, if you have completed the above steps 1 to 3 successfully. For example, following command would mount "data" folder in container with a shared-storage "/home/docker/tirthal-projects/temp" on docker machine. Here, the "/home/docker/tirthal-projects" is mounted with the host Window machine's folder (i.e. D:\Tirthal-LABs\).

docker run -v /home/docker/tirthal-projects/temp:/data --name <container-name> <image-name>



References: