Skip to content
This repository has been archived by the owner on Sep 19, 2021. It is now read-only.

Docker Volumes with Babun

Sebastián Ramírez edited this page Nov 12, 2015 · 2 revisions

Docker Volumes with the Docker Toolbox

Here's the situation, Docker is made to run in a Linux host, but to make it run in Windows and Mac OS, Docker Toolbox creates a Virtual Machine, using docker-machine to create a VirtualBox machine with "Boot2Docker" installed. To allow some Docker Volumes working, the Docker Toolbox creates a "shared folder" in VirtualBox, sharing the Windows C:\Users directory in the /c/Users/ directory inside that Boot2Docker VirtualMachine.

So, when you test in your local Windows there are two directory mappings taking place, one from your Windows to your VirtualBox VM and one from that VM to the container.

As Docker Toolbox comes with Git Bash, which uses MINGW instead of Cygwin, the Drives (and their subdirectories) in Windows appear with their letters under the root directory of MINGW, so C:\Users\ becomes /c/Users/, and that directory is mounted in the VirtualMachine in the /c/Users/ directory.

So, if your code is in C:\Users\myname\code\superapp\ in Windows, when you do a pwd in MINGW (Docker Toolbox Quickstart Terminal, Git Bash) it will show /c/Users/myname/code/superapp/, and inside the VirtualMachine that is shared exactly in /c/Users/myname/code/superapp/, so, although it seems Docker is mounting your local directory as a volume, it is actually mounting the directory that is inside the Virtual Machine, that in turn is a shared folder from your Windows.

Now, in Cygwin world (Babun) the Drives appear under the /cygdrive/ directory. So, if your code is in C:\Users\myname\code\superapp\ in Windows, when you do a pwd in Cygwin (Babun), it will show /cygdrive/c/Users/myname/code/superapp/, but inside the VirtualMachine that is shared without the "cygdrive" part, it is in /c/Users/myname/code/superapp/.

In my case, my code in Windows is actually in another Disk Drive, in D:\Sebastian\code\, so it doesn't work either way, because the shared folder inside the virtual machine is only C:\Users\.


2015-11-12 Note:

In the latest version of babun-docker all this is handled automatically, all the Windows drives are set up as VirtualBox shared folders and are mounted inside the virtual machine (docker-machine) inside a directory under /cygdrive, so all this "just works" now. The rest of the contents of this wiki are left as they are just for reference.

To update babun-docker, run:

babun-docker-update

Configuring Babun for Docker volumes

(Actually, configuring the VirtualBox virtual machine)

To allow using "volumes" from other directories (not in C:\Users\) or using the volumes with Babun with $(pwd) you can do the following:

  • Open VirtualBox
  • Right click your "default" virtual machine
  • Settings -> Shared Folders -> Right click -> Add Shared Folder
  • In "Folder Path" select the Windows folder that you want to share, for example D:\Sebastian\code (or it could be C:\, etc).
  • In "Folder Name" you can use the name you want, it won't be mounted in your VM yet, but you have to remember that name, in my case, it is "code"
  • Select "Auto-mount" and "Make Permanent" and hit "OK"
  • The new shared folder shows up in your VM settings, hit "OK"

Now it is shared in your virtual machine, but is not mounted yet. To mount it:

  • Open Babun and go to the directory that you want to share, for example, in my case:
cd "D:\Sebastian\code"
  • From there, we need to know how that path is called in Cygwin "world", with "pwd", so run:
pwd

In my case, that returns:

/cygdrive/d/Sebastian/code
  • From Babun (or MINGW) you will connect to your Virtual Machine with ssh, using the docker-machine program (you will probably have to run the following steps every time you restart your VM), so run:
docker-machine ssh default

Note: if your virtual machine "default" is not running, you can start it with:

docker-machine start default
  • After connecting with SSH to your VM, you have to become the root user:
sudo su
  • First, you have to create a folder with the same path as the one that you want to share (as named by Cygwin, Babun), so, in my case:
mkdir -p /cygdrive/d/Sebastian/code
  • Now, since we created a "shared folder" in VirtualBox, that virtual machine can "mount" that shared folder, to do that, you have to use the mount command. And you have to use the "shared name" from VirtualBox and the directory that you just created (the last two parameters, in my case: code and /cygdrive/d/Sebastian/code), for example:
sudo mount -t vboxsf code /cygdrive/d/Sebastian/code
  • From the virtual machine, you should be able to see that directory with your Windows contents:
ls /cygdrive/d/Sebastian/code

Note: You will have to mount the shared folder every time, following the steps from the SSH part. I tried modifying the /etc/fstab file, to make the changes permanent, but it gets reset to default after restarting the machine.


Alternative steps to mount the shared folder

Note: this steps are intended to make the changes permanent, but once the VM is restarted they get reset to defaults, so it's probably worthless to follow them.

  • Now, since we created a "shared folder" in VirtualBox, that virtual machine can "mount" that shared folder, to do that, and to make it permanent, we have to modify /etc/fstab, but first, create a backup:
cp /etc/fstab /etc/fstab.orig
  • Create a new line adding the mount point of your shared folder. And you have to use the "shared name" from VirtualBox and the directory that you just created (the first two parameters to put in /etc/fstab, in my case: code and /cygdrive/d/Sebastian/code), for example:
echo "code /cygdrive/d/Sebastian/code vboxsf defaults 0 0" >> /etc/fstab
  • Mount everything declared in the /etc/fstab:
mount -a
  • From the virtual machine, you should be able to see that directory with your Windows contents:
ls /cygdrive/d/Sebastian/code

Testing your new Docker volumes from Babun

Now that you have everything set up, you can test a new docker container with volumes.

  • In Babun, from the directory that you just shared with the steps above (in my case: /cygdrive/d/Sebastian/code), create a container that uses your current directory (or any directory inside the shared folder) as a volume:
docker run -it -v $(pwd):/var/www ubuntu bash
  • Inside your container, you can check that the contents are there, for example:
ls /var/www/