I've just set up boot2docker on my Mac. How do I go about mounting a local directory on my Mac so that it's accessible all the way through to a running Docker container? Thanks for your help!
-
1Checkout the boot2docker docs, it has some stuff about just this. – cpuguy83 Jun 14 '14 at 03:46
4 Answers
As of October 16, 2014, Docker supports mounting directories in the /Users path seamlessly.
From the Docker blog:
With this release we are addressing the most common issue: sharing directories between your Mac and your containers. Using Docker 1.3 with the corresponding version of boot2docker, host-mounted volumes now work the way you expect them to.
...Note that there are still some limitations: for example this feature is limited to boot2docker’s virtualbox configuration, cannot be managed dynamically, and only works for directories in /Users . But we are receiving exciting contributions to improve volume management, so expect this area to improve drastically in the next few releases.
Example usage:
$ docker run -v /Users/bob/myapp/src:/src [...]

- 1
- 1
-
Is this supported on Linux too? Googles `gcloud` tools requires boot2docker and doesn't even provide a way to pass command line arguments to Docker. – Günter Zöchbauer Nov 18 '14 at 06:50
-
1I've only used Docker natively on Linux, so I'm not sure. Might be worth opening a separate question for. – Nov 18 '14 at 13:28
-
1This only works if the directory is inside your User folder. If it isn't (e.g. if it's on another hard drive), this won't work. – Matt3o12 Dec 21 '14 at 14:42
-
It works, but you have to know that virtualbox shared folder are very slow. it's better to configure a standard network shared folder. – Jokin Sep 14 '15 at 14:23
-
To get this working on Docker 1.8 with Docker-toolbox, I had to use the full path. docker run -v ./dir:/dir [..] did not work. Yet docker run -v /Users/bob/dir:/dir [..] did. – xer0x Sep 29 '15 at 22:23
boot2docker with share other than /Users
see https://github.com/boot2docker/boot2docker/issues/678.
Share your folder with the VM:
VBoxManage sharedfolder add boot2docker-vm --name /tmp/Work --hostpath /CODE --automount
Based on info found un bootscript.sh
, you know that the VM will run a bootlocal.sh
script that is in the /var/lib/boot2docker
folder, where data persists.
Add a file /var/lib/boot2docker/bootlocal.sh
#!/bin/sh
# bash is not available!
mkdir -p /CODE
mount -t vboxsf /tmp/Work /CODE
Then chmod +x /var/lib/boot2docker/bootlocal.sh
and reboot your boot2docker-vm
vm.

- 23,846
- 6
- 82
- 104
boot2docker together with VirtualBox Guest Additions
How to mount /Users into boot2docker
tl;dr Build your own custom boot2docker.iso with VirtualBox Guest Additions (see link) or download http://static.dockerfiles.io/boot2docker-v1.0.1-virtualbox-guest-additions-v4.3.12.iso and save it to ~/.boot2docker/boot2docker.iso.

- 8,936
- 5
- 48
- 73
As Levi mentioned, the /Users directory is auto-mounted. This is true in both boot2docker and docker-machine. That said, if you want to mount anything outside of /Users, all the current answers talk about Boot2docker. Since that's now deprecated in favor of docker-machine, this works for docker-machine:
First, ssh into the docker-machine vm and create the folder we'll be mapping to:
docker-machine ssh $MACHINE_NAME "sudo mkdir -p \"$VOL_DIR\""
Now share the folder to VirtualBox:
WORKDIR=$(basename "$VOL_DIR")
vboxmanage sharedfolder add "$MACHINE_NAME" --name "$WORKDIR" --hostpath "$VOL_DIR" --transient
Finally, ssh into the docker-machine again and mount the folder we just shared:
docker-machine ssh $MACHINE_NAME "sudo mount -t vboxsf -o uid=\"$U\",gid=\"$G\" \"$WORKDIR\" \"$VOL_DIR\""
Note: for UID and GID you can basically use whatever integers as long as they're not already taken.
This is tested as of docker-machine 0.4.1 and docker 1.8.3 on OS X El Capitan.

- 36,793
- 40
- 144
- 207