Docker: Permanently Mount a VirtualBox Shared Folder

While this technique works for both Windows and OSX, most Windows developers I know don’t keep their source code in the Users folder on the system drive. Historically there have been too many problems:

  • The path used to start with C:\Documents and Settings\[USER_NAME]\
  • msbuild blows up when your path is longer than 256 characters
  • There’s no convenient ~/ command-line shortcut when typing the path on the command-line.

However, that’s the only default path you get when you create a boot2docker image for VirtualBox.

In my last post I showed how to mount a VirtualBox shared folder but as soon as you restart (that never happens on a Windows box) you’ve lost it and have to create the mount all over again.

Mount a shared folder

Just to review, so you don’t have to read the last post:

  1. Create a shared folder in VirtualBox:

  2. ssh into your Guest OS (Docker VM):

    $ docker-machine ssh defualt
  3. Create a new folder for your mount:

    $ sudo mkdir -p /mnt/src

    The -p is just in case /mnt doesn’t already exist.

  4. Add your new mount:

    $ sudo mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` src /mnt/src

    id -u docker and id -g docker will use the user id and group id respectively. You could just enter 1000 and 50 respectively and you’d be fine. I think the longer version is more clear and safe.

    You should now be able to view the contents of your directory within your Docker VM:

    $ ls /mnt/src

Make It Permanent

The problem is everything gets erased whenever you restart your VM. What we need is a setup script that won’t be erased and will run on startup. It turns out it’s pretty easy. Create a file named at the following location:

$ sudo touch /mnt/sda1/var/lib/boot2docker/

Now add the following to the file (no sudo needed):

mkdir -p /mnt/src
mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` src /mnt/src

Make sure you save and close the file. That’s it! If you exit your ssh session, then restart your VM:

$ docker-machine restart default

Once it’s finished, log back in and verify it’s there:

$ docker-machine ssh default
$ ls /mnt/src

You should see the contents of your shared folder.