Share volumes in a Docker Container on MacOS
This post shows how to share any MacOS volume in a Docker container. We use docker-machine together with VirtualBox on MacOS. See an older post of mine how this can be done.
Share user home directory
Let’s map a directory in the user home /Users/peter/Hadoop
to /usr/local/hadoop/test-home
in the Docker container. This should be easy, as the user directory is mounted
by boot2docker.
Step 0: Setup
First initialize the directory and create a test file test.txt
in it that can be
used to test if the mapping was successful:
Step 1: Map the volumes
Run Docker and map this directory to /usr/local/hadoop/test-home
in the
Docker container. For this we use the -v
option as follows:
Check Docker mounts:
Check the home directory in boot2docker:
Check the directory in the Docker container:
/usr/local/hadoop/test-home
was generated in the Docker container and we
find the test.txt
file.
This was easy, the -v
did the job.
Share any directory
Mapping a directory in the user home was easy, as this directory is mounted automatically in boot2docker. However, this is not the case for directories outside of the user home directory as we will see below.
Step 0: Setup
First, let’s see what’s in the MacOS /Projects/hadoop
directory that we want to map:
In our case, we are especially interested in the build
directory with the
generated Hadoop Map/Reduce libraries we want to use in the Hadoop Docker
container.
Step 1: Map the volumes
Start Docker and map the project directory /Projects/hadoop
to
/usr/local/hadoop/test-project
as above:
Check Docker mounts:
This looks as expected.
List the mapped volume in the Docker container:
The directory was generated in the Docker container but it is empty!
The reason for this is as mentioned above, that only the user home directory is mounted in boot2docker per default and not any other.
Two additional steps are needed:
- Map the volume in VirtualBox
- Mount the volume in boot2docker
Step 2: Map the volume in VirtualBox
Stop VirtualBox with docker-machine stop default
and share the
/Projects/hadoop
folder with:
Start the VirtualBox again with docker-machine start default
.
Alternatively, you may use the VirtualBox UI (Settings -> Shared Folders).
Step 3: Mount the volume in boot2docker
Use mount
to mount the MacOS directory in boot2docker.
Usage:
where:
DEVICE
is the logical name of the shared folder defined in VirtualBoxNODE
is the hostpath of the directory to be shared in boot2docker (not MacOS!)
Use it:
Note, that DEVICE
must correspond to the logical name of the shared folder defined
in VirtualBox above. If you missed the name, then following error message is returned:
NODE
must correspond an existing file path in boot2docker (not MacOS!). If you missed this name, then
following error message is returned:
Run the Docker container again as above:
Note, that /hadoop
corresponds to NODE
set above (not the original path in MacOS!).
List the project directory in the Docker container:
Yes, it works. However, if you restart boot2docker, the mounted volumes disappear.
Step 4: Permanently mount MacOS volumes in boot2docker
In boot2docker, edit/create (as root) /mnt/sda1/var/lib/boot2docker/bootlocal.sh
to
add an automount:
More information
See this StackOverflow post.