Docker - Volumes (Mac OSX)
#How Docker Works ON OSX
On a Linux installation Docker runs directly on the host but on MAC OSX it runs in a slightly different manner. Instead of running directly on the host, docker runs on a small light weight scaled down Linux virtual machine named ‘default’. The docker daemon is running on the linux VM and you use the docker client to connect to it.
If you execute the following command in the shell you will see that the IP address returned is different from the HOST machine.
#Overview of Volumes in OSX
- Virtual Box APP is installed on OSX.
- Boot2Docker is launched in a virtual machine named ‘default’ on VirtualBox.
- “docker run” command run the docker image in a container on the “default” VM.
Volume Mounting :
A directory present on the OSX (host machine) is first mounted to a directory of the same name(need not be of the same name) on the VM. This ensures access of the host(OSX) directory contents to the guest OS (boot2docker ‘default’ VM). Now when the “docker run” command is executed with the volume parameters, it is the “directory” on the guest OS (boot2docker ‘default’ VM ) which gets mounted onto the container.
Example with different directory names:
#Volumes in OSX
Containers are nothing but images with a read-write layer on top of it. This read-write layer enables the process running in the container to read and write files during its run. Once the container exist, the data written to the file system is removed.
Volumes help bring in data persistence to containers. Data stored in volumes are persisted even after the container has exited. Volumes are nothing but a directory present on the HOST machine which is mounted to the container. Any information that the container writes to this mounted directory or in this case called VOLUME, persists independent of the container lifecycle.
Below is an example of how to mount volume while starting the container.
The -v command param is asking docker to make the ‘docker-shared’ directory present in the current location and expose it as ‘mounted-volume’ at root location of container VM.
This would be a straightforward task in Linux installations but in OSX, since Docker is running on a Linux VM, we would have to create a mount pipe between the HOST and container through the Linux VM.
#Steps(With VitualBox as provider) :
1 . Open up the Virtualbox UI and find the ‘default’ machine.
2 . Click on ‘Shared folders’.
3 . Add a new shared folder. Set “Folder Path” to a directory on OSX that you would like to use as Volume. Provide a name for the folder. In this example, I have provided “docker-shared” as the name for the folder.
4 . Enable ‘Auto Mount’ and ‘Make Permanent’.
5 . Log into the running ‘default’ machine using the following command. This should log you into the ‘default’ Linux VM’s shell.
6 . Execute the following command and note down the GID and UID values.
7 . Create a directory named ‘docker-shared’ on the Linux VM
8 . Execute the following command to mount the host directory previously shared to the VM
Syntax : sudo mount -t vboxsf -o defaults,uid=