Boot2Docker on Mac - Accessing Local Files - macos

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.

Related

Can't mount Windows host directory to Docker container

I'm on Windows 10 Pro with Docker Version 1.12.0-rc3-beta18 (build: 5226). I would like use Docker for PHP development on Windows machine. I tried all possible (I hope) variations of mounting host directory into Docker container:
//c/Users/...
/c/Users/...
//C/Users/...
/c/Users/...
c:/Users/...
c:\Users...
"c:\Users..."
Neither of variants launch container. Yes, docker run creates container and I can see it with docker ps --all. But I can't it start. E.g. I tried simple documentation example:
docker run -d -P -v "C:\temp":/opt/webapp training/webapp python app.py
and
docker logs e030ba0f7807
replays as
python: can't open file 'app.py': [Errno 2] No such file or directory
What happened?
If you are using docker with docker-machine, you would need to register c:\temp first as a shared folder in VirtualBox.
See "docker with shared folder d drive"
From within a docker-machine ssh session:
sudo touch /mnt/sda1/var/lib/boot2docker/bootlocal.sh
Add to that file:
mkdir -p /mnt/temp
mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` temp /mnt/temp
That path would then be accessible through /mnt/temp for instance.
The same applies for C:\Users, which is already a shared folder c/Users.
It is accessible with /c/Users.
With Hyper-V, see "Running Docker on Hyper-V" from Henning M Stephansen:
Hyper-V is a more isolated and restrictive environment than VMWare or VirtualBox is, so there’s no concept of shared folders.
However we can mount and access Windows shares from our Docker VM.
The first thing you need to do is to share a folder. This folder can be restricted to just your user.
If the VM has access to the network through an External Virtual Switch or an Internal Virtual Switch you should be able to mount your folder from the docker VM.
To be able to mount a windows share from Boot2Docker/Tiny Core Linux we need to install some additional module (This might be included in your image):
wget http://distro.ibiblio.org/tinycorelinux/5.x/x86/tcz/cifs-utils.tcz
tce-load -i cifs-utils.tcz
Now we can mount the shared folder using the following command
sudo mount -t cifs //HOST-IP-HERE/SharedFolderPath /path/where/we/want/it/mounted -o username=HOST_USERNAME_HERE

Volume sharing in Docker and Virtualbox

I have installed docker toolbox on my Windows PC,when i install containers on it using Kitematic, I want to share a folder with my local machine. However docker runs in virtualbox on the default machine in VirtualBox.
So, i guess i first have to do a fvolume share of the virtualbox vm with my local machine and then a second share of the docker container with the folder that I shared in virtualbox. Could someone assist me in this please?
No, you shouldn't need to share any more folder: docker-machine will create a VM which auto-mount C:\Users\<yourLogin>.
docker-machine ls
docker-machine ssh default
cd /C/Users/<yourLogin>
You can mount other folders from your hosts, but you don't have to.
I'm running docker version 18.03.0 ce on Windows 10, and the C:\Users folder is shared as /c-users on the docker VM (default), so using the command line
docker run -it -v /c-users/my_windows_user/project:/media/project ubuntu:16.04 bash
I can access the Windows project files from /media/project within the Ubuntu container

How run Docker with sync folder on Windows

Well using Kitematic on Windows and starting the container hello-world-nginx. I'm totally able to edit the file on Windows and see the modifications at the browser.
But when I create some image and try to edit the file (html), nothing happens.
Here's the command:
docker build -t a-example . && docker run --name a-example -i -t -v //c/Users/pablo/htdocs/www/a-example/src:/www -d -p 80:80 a-example
And here's my Dockerfile:
FROM nginx
RUN mkdir /etc/nginx/logs && touch /etc/nginx/logs/a-example.log
ADD ./nginx.conf /etc/nginx/conf.d/default.conf
ADD /src /www
VOLUME /www
Another question:
Is this message have something to do with?
SECURITY WARNING: You are building a Docker image from Windows against
a non-Win dows Docker host. All files and directories added to build
context will have '-r wxr-xr-x' permissions. It is recommended to
double check and reset permissions f or sensitive files and
directories.
I do not use KiteMatic or Docker Machine to control my local docker instance.
However, Docker Machine uses a boot2docker iso (customized version of Tiny Core Linux with Docker) running in a VirtualBox vm. Virtual Box provides guest Additions to mount host machine folders.
Refer to Boot2Docker guidelines at the time of Docker 1.9.1 release
Or the Docker Machine shares:
On Windows:
- C:\Users mounted as /c/Users
On Linux:
- /home mounted as /hosthome
On OSX:
- /Users mounted as /Users
which is done through Guest Additions:
vbm("sharedfolder", "add", d.MachineName, "--name", shareName, "--hostpath", shareDir, "--automount")
If you opt to use samba shares from your windows host instead, you will need the appropriate TLC packages (from the FAQ):
How do I mount windows shares?
If the sharing computer is at least Windows 2000, you can mount the share with only the filesystems-2.6.29.1-tinycore.tcz extension.
In a root console:
mkdir /mnt/windows
mount -t cifs //9.53.216.11/myshare /mnt/windows -o user=myname,pass=mypassword
And after searching for boot2docker and filesystems-2.6.29.1-tinycore.tcz I got to this GitHub issue:
sudo find / -type d -name 'cifs' seems to indicate that CIFS (Samba) client in the right kernel version is already there... allowing us to run
sudo mount -t cifs //MYPC/hostgithub ~/windows -o user=USER
on the boot2docker VM
Using Docker Volumes
It seems Docker Volumes are a new proposal for handling host folders for Developer Environments, see:
Docker-Volume-Netshare: Although this requires nfs-common or nfs-utils (depending on your distro) to be available on the docker machine, this may not work for CoreOS / Boot2Docker without some other tricks.
It should actually work on boot2docker considering how Docker-Volume-Netshare just wraps shell exec /bin/sh -c "mount -t cifs ..." command. If you use the so0k/mount.cifs_copy on CoreOS you may need a way to specify the path to cifs /opt/bin/mount.cifs.
For Windows users, note also my blog post using the netshare volume driver mentioned above to provision mounts from the Windows host across a Swarm Cluster.
Note: Be careful when using git-for-windows and git-bash as I'm having issues with these converting the paths I pass on to docker.exe
I don't think your volume is available so your container is displaying the static content from /src that you ADDed during the image build.
Kitematic runs a VirtualBox linux machine which runs your docker containers.
The path you have used for the volume: //c/Users/pablo/htdocs/www/a-example/src:/www looks suspect as that's the full windows path.
Docker machine (which is what kitematic uses underneath to manage the virutal machine) will map C:\Users to /Users by default
Try --volume /Users/pablo/htdocs/www/a-example/src:/www

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.

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.
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.

Resources