Mount volume to Docker image on OSX

geowa4 picture geowa4 · Oct 13, 2014 · Viewed 9.5k times · Source

On a Mac, how do you mount a volume to a Docker container?

On my linux box, this is easy. All I need to do is something like -v /src/webapp:/opt/webapp when running the container. But Mac is different since I have to run boot2docker to run a VM in VirtualBox. I've tried running

boot2docker init
boot2docker up
boot2docker ssh # to poke around
boot2docker stop
VBoxManage sharedfolder add "boot2docker-vm" --name "Users" --hostpath /Users
boot2docker up
boot2docker ssh "sudo modprobe vboxsf"

but I get

modprobe: module vboxsf not found in modules.dep

If I ignore that and still try to mount on the VM like so

boot2docker ssh "sudo mkdir /test && sudo mount -t vboxsf Users /test"

I get

mount: mounting Users on /test failed: No such device

I feel like I'm missing something extremely simple, but I can't quite figure it out. Any help would be greatly appreciated.

Answer

geowa4 picture geowa4 · Oct 15, 2014

Ok, after digging through a GitHub PR, I was able to figure out a way to do this. For the future readers out there, this process should be fixed in an upcoming release of boot2docker.

# assuming boot2docker and VirtualBox are installed
wget http://static.dockerfiles.io/boot2docker-v1.2.0-virtualbox-guest-additions-v4.3.14.iso
mv boot2docker-v1.2.0-virtualbox-guest-additions-v4.3.14.iso ~/.boot2docker/boot2docker.iso
# blow away your old boot2docker-vm if it exists (boot2docker down && boot2docker destroy)
boot2docker init
boot2docker up
# set DOCKER_HOST as instructed
boot2docker stop
VBoxManage sharedfolder add boot2docker-vm --name /Users --hostpath /Users
boot2docker up
# if you ssh into the VM now, you'll notice /Users is present, but empty; I don't know/care why.
boot2docker ssh "sudo mount -t vboxsf -o uid=1000,gid=50 /Users /Users"
# done

This worked for me so I hope it works for others. In the near future, I expect this issue to be solved by boot2docker, especially since the PR from which I got these commands was merged.

EDIT: boot2docker 1.3.0 supports this without any further changes. After updating, I ran these commands:

boot2docker destroy  # start over
boot2docker download # download the udpated ISO
boot2docker init
boot2docker up
# done