Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mount volume to Docker image on OSX

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.

like image 818
geowa4 Avatar asked Oct 13 '14 20:10

geowa4


People also ask

Can you mount a volume while building your docker image?

When building an image, you can't mount a volume. However, you can copy data from another image! By combining this, with a multi-stage build, you can pre-compute an expensive operation once, and re-use the resulting state as a starting point for future iterations.

How do I mount a volume in running docker container?

Cloning From An Existing Container But, if you do need to add a volume to a running container, you can use docker commit to make a new image based on that container, and then clone it with the new volume. Then, you can run the new image, replacing the old image with the cloned one.

Where are docker volumes stored Mac?

As a Linux user, you learn that Volumes are stored in a part of the host filesystem managed by Docker, and that is /var/lib/docker/volumes . When you're running Docker on a Windows or Mac OS machine, you will read the same documentation and instructions but feel frustrated as that path don't exist on your system.


2 Answers

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
like image 189
geowa4 Avatar answered Sep 22 '22 20:09

geowa4


For folks finding this in the future who want to mount anything other than /Users, there's a script someone made as a gist on github that does the whole process for you and is awesome. Just use this. It saved me a lot of headache of having to keep screwing around with virtualbox. This is tested as of Docker 1.3.0 on my Mac running Yosemite.

EDIT:

Now that docker-machine cli has been deprecated in favor or docker-machine, here's how you can do it with 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.

like image 22
Eli Avatar answered Sep 18 '22 20:09

Eli