Mount volume to Docker image on OSX - macos

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.

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

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.

Related

Cannot mount a volume without using sudo

The following command works and mounts the local volume:
sudo docker run -ti -v "$PWD/codebase/realsmart-saml-copy":/var/www/html realsmart-docker_smartlogin bash
The following command does not work and does not mount the volume
docker run -ti -v "$PWD/codebase/realsmart-saml-copy":/var/www/html realsmart-docker_smartlogin bash
For some reason, docker is only able to mount volumes using the sudo command, rendering our local docker environment useless on a colleagues laptop. The same docker-compose file works on my laptop (also a mac, same OS).
Any idea as to what the issue might be with his laptop configuration? Or indeed the docker setup.
(The code extract is to make clear the problem with mounting volumes, the same issue presents itself using a compose.yml file.)
Non working code:
docker run -ti -v "$PWD/codebase/realsmart-saml-copy":/var/www/html realsmart-docker_smartlogin bash
No error messages are displayed, but the results are not as expected as the volume does not mount without using sudo.
Try to see if the user is part of the docker group.
It would make sense that sudo works, but not for the local user, if that local user is not part of the docker group.
The solution for anyone interested.
After upgrading to Docker Desktop Boot2Docker has been replaced.
Steps to fix the issue:
docker-machine rm machine-name
unset DOCKER_TLS_VERIFY
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
unset DOCKER_HOST
restart Docker Desktop
cd path/to/docker-project.
docker-compose build
docker-compose up (or docker run)
project now available on localhost
Further details: https://docs.docker.com/docker-for-mac/docker-toolbox/
Add your user to the docker group.
sudo usermod -aG docker $USER

How to Setup a Docker Tomcat Container on Mac OS X

I'm new to Docker...
From my understanding, Docker is only compatible with Linux, is it possible to run docker for development purposes on a Mac?
I installed virtualbox using homebrew and have tried to create a virtualbox instance. I installed docker, but am having trouble getting my mac to communicate with the vm docker instance. My end goal is to get a locally debuggable instance of tomcat running on the docker container.
Any help or tips would be helpful.
Information:
Because Docker only runs on Linux you will need to install some kind of virtual instance on your local machine. An easy and popular way to do that is to install
Boot2docker and VirtualBox. VirtualBox is a dependency of Boot2docker. You can download, setup and install the latest versions from their websites or if you are using Homebrew, as you mentioned, you can quickly get the working binaries both in one step.
After installing boot2docker, you're ready to use Boot2docker to create a Tomcat Container. You can find a pre-configured tomcat image by searching Docker's community repository, docker hub registry.
Notes:
Each time you execute the docker run command a new container is created.
The VM running Docker requires a ssh private/public encryption key handshake to connect to. If you follow my steps below, one will be generated for you.
Steps to Setup Tomcat using the tomcat image:
Open Terminal and run this command: brew install boot2docker
Create a new Boot2Docker VM instance using the init command: boot2docker init
Run this command in Terminal to forward local ports to the vm:
for i in {10000..10999}; do VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port$i,tcp,,$i,,$i”; VBoxManage modifyvm "boot2docker-vm" --natpf1 "udp-port$i,udp,,$i,,$i";done
Start the boot2docker daemon: boot2docker start or boot2docker up
After starting docker, copy the exports that are displayed from the previous command to your clipboard
Edit your bash profile file ~/.bash_profile (or if you are using zsh, edit the resource configuration file ~/.zshrc) with a text editor (I prefer using Sublime text): subl .zshrc *note: this will permanently save the docker env variables.
Paste the exports into that file and save
Execute the source command on the file: source .zshrc
Pull the latest tomcat image to create a container and start tomcat: docker run -it --rm -p 10080:8080 tomcat:8.0 *note: this will forward your local 10080 port to the vm's 8080 port.
Go to http://localhost:10080, you should see the tomcat start page!
Useful Docker commands:
$ boot2docker status
$ docker version
$ docker ps #shows running containers
$ docker ps -a # shows all containers
$ docker exec -it NAME /bin/bash #to start a bash session on the container. -i = interactive, -t = tty
External Resources:
https://docs.docker.com/installation/mac/#installation
https://registry.hub.docker.com/_/tomcat/
Most people use boot2docker to run on Macs. You may also want to take a look at Kitematic, which gives you a GUI to play with.
Finally, the future is probably to use docker machine, which can provision a VM for you.
Docker requires Linux Kernel features, hence it cannot be run natively on OSX.
See instead Boot2Docker. This link gives you instructions on how to get going.

boot2docker changing IP address from default 192.168.59.103 to 192.168.59.104 (Mac)

boot2docker was running all the time on 192.168.59.103.
Suddenly the command boot2docker ip gives 192.168.59.104.
The problem is that now I have to change all my /etc/hosts entries.
How can I make the boot2docker IP address static?
Side question: Should I avoid boot2docker completely and run everything inside a Vagrant machine?
I am running on Mac OS X v10.10 (Yosemite).
If you haven't already done so, create a Docker configuration with:
boot2docker config > ~/.boot2docker/profile
Then edit your Docker configuration with the following line:
UpperIP = "192.168.59.103"
Then follow the other steps to clear out your old Docker images and start a new one.
I was also having issues connecting to Docker, but a system restart resolved them.
In order to remove the VM and restart it again like #ramden suggested you could do:
You can remove all containers from the current environment by running
$ docker rm $(docker ps -a -q)
To remove all images:
$ docker rmi $(docker images -q)
Restart the VM, cleaning all cache from old images, doing the following
$ boot2docker delete
$ boot2docker init
$ boot2docker start
I've just encountered the same issue and couldn't find an answer right away, maybe this is worth for future interest.
Cheers.
After removing the virtual machine from the VirtualBox GUI (delete everything) it works again on 103. I guess something messed up with boot2docker while using it.

boot2docker on windows - Can't access exposed port

I've installed boot2docker (full install) on Windows 7 and am trying to run the container port redirection demo:
docker run --rm -i -t -p 80:80 nginx
Which looks like it isn't quite finishing properly, it just stops and looks like this:
When I open another git bash shell and run boot2docker ip I get 192.168.59.103, and when I pop that in chrome I get Error code: ERR_CONNECTION_TIMED_OUT
It works fine for me with plain docker on Ubuntu 14.04. What else do I need to do to make it work with boot2docker on windows?
Looking more closely, my problem is the same as this question: Docker, can't reach “rails server” development from localhost:3000 using docker flag -p 3000:3000
The answer to that question that worked for me was this one, which simply says to run
boot2docker ssh -L 8080:localhost:80
at the terminal before starting boot2docker
In my case I do this (from a git bash terminal):
boot2docker init # from https://github.com/boot2docker/boot2docker
boot2docker up
boot2docker ssh -L 8787:localhost:8787 # sets up port forwarding and starts boot2docker
docker run -d -p 8787:8787 cboettig/rstudio # starts the container I want
then go to my web browser in windows and point it to http://localhost:8787/ and I get a server instance of RStudio. When I'm done:
docker rm -f $(docker ps -a -q) # delete all containers
UPDATE: downgrading to an earlier version of VirtualBox will fix this
After struggling with folder sharing I regressed through previous versions of VirtualBox and found that with version 4.3.12 I could enable folder sharing and have the port forwarded exactly according to the official instructions, that is I could access my docker container at 192.168.59.103. So downgrading VirtualBox is another option for working around this problem.
ANOTHER UPDATE: updating to the new release of v1.3.1 of boot2docker will fix this
This release just came out a week ago and includes VirtualBox Guest Additions, which simplifies all of this. I now simply do
boot2docker ssh # start boot2docker
docker run -d -p 8787:8787 -v /c/Users/foobar:/home/rstudio/foobar rocker/rstudio
And I get everthing working as expected and can log into RStudio in my browser at http://localhost:8787/ (linux) or http://192.168.59.103:8787 (Windows) and it just works.
In this case I've also got folder sharing working with, /c/Users/foobar corresponding to an existing folder on my computer at C:/Users/foobar, and foobar can be anything. With this method I can read and write files both ways between Windows and RStudio and I don't need to connect to a special IP address like the samba method does in the official docs
I had this problem too after a couple of failed attempts to boot2docker start. This created multiple entries of host-only networks configured on VirtualBox (VirtualBox Host-Only Ethernet Adapter #2, VirtualBox Host-Only Ethernet Adapter #3), and probably the boot2docker's VM was using a bad one.
I cleaned up using Virtualbox standard UI, leaving only one of the networks and now everything works fine.
I'm using boot2docker 1.5.0.
Just to register something that happened to me, and made me lose a couple of hours.

Boot2Docker on Mac - Accessing Local Files

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!
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 [...]
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.
boot2docker together with VirtualBox Guest Additions
How to mount /Users into boot2docker
https://medium.com/boot2docker-lightweight-linux-for-docker/boot2docker-together-with-virtualbox-guest-additions-da1e3ab2465c
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.
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.

Resources