Accessing the host file system from a Docker container on OS X or Windows

Mounting (or sharing) a directory from the Docker daemon host into a container is simple enough. Example:

$ docker run -v [host directory path]:[container directory path] -it [image name]

However, on OS X and Windows, the Docker daemon (currently) runs inside of a Linux VM, that by default is run using VirtualBox. (Since Docker only runs natively on Linux) So, you’d be mounting a folder from the Linux VM, which is probably not what you want if you want to share files from your host machine into the container.

Thankfully, VirtualBox allows sharing of folders from your host machine into the VM where the Docker daemon resides. The Docker Linux VM is also auto-configured to mount either /Users (OS X) or C:\Users (Windows) into /Users on the Linux VM:

This means we can mount /Users from the Linux VM into a container, allowing it access to your underlying host/physical system’s files in /Users (OS X) or C:\Users (Windows).

Here’s an example I used when playing around with the TensorFlow Docker image:

$ docker run -v /Users:/host/Users -it b.gcr.io/tensorflow/tensorflow

This mounted /Users (from the local host file system) into /host/Users, so I had access to source code files I was editing. (You could also make the container mount the file system as read-only by appending :ro to the -v argument.)

To share other directories on your host system, you’d just need to add those to the Shared Folders for the VirtualBox Linux VM, then use another -v argument to the docker command to mount them into the container.

Note that with this method you’re going through two levels of mounting/sharing: From the ultimate host system to the Linux VM (via VirtualBox’s shared folders) and then from the Linux VM to the container. The first is likely to have a performance impact, so I wouldn’t use it in production. Then again, for production you should probably be using the native Linux version of Docker.

References

  1. Manage data in containers: Docker Fundamentals

One Comment »

  1. Great article. This Really helped me. Other documentation was really convoluted and I was trying for ages to find out how to mount a drive in order to do a mongorestore. After reading yours I managed it no problem! Thanks!

Comments are now closed for this entry.