How do I mount a directory from the host machine to a container in Docker?
6 Answers
Just as a final update, this feature is now released in Docker (though the API has changed since the pull request linked by @imiric).
Simply use a command like
docker run -v /tmp:/root myImage
in order to mount /tmp
from the host machine as /root
within the image.
Source: https://docs.docker.com/engine/userguide/dockervolumes/

- 3,580
- 44
- 47

- 2,028
- 2
- 15
- 7
-
2Q: Will changes to any files within those paths work both ways? – Alix Axel Oct 19 '13 at 02:34
-
2@AlixAxel yes they will. The directory and files reside at the host which is mounted to the container. You can edit the files from either side and see the changes instantly in both. I use this to bring dynamic content into containers without bloating them. – Vilsepi Oct 30 '13 at 12:05
-
10Note that this doesn't work as expected on OS-X (and possibly Windows), as Docker uses a VirtualBox image running from ramdisk as the host, so the command mounts an (empty) folder in the VB host within the image. – Peter Gibson Mar 03 '14 at 22:04
-
@PeterGibson, maybe using vboxmanage one can add a shared folder, is that possible, what do you think? – Sebastian May 07 '14 at 12:25
-
8This definitely does not work on OSX using boot2docker. – Daniel Coffman Jun 11 '14 at 07:57
-
2This partially works on OS X using boot2docker as of [1.3](http://blog.docker.com/2014/10/docker-1-3-signed-images-process-injection-security-options-mac-shared-directories/). It works for mounts in the /Users directory. – kojiro Oct 29 '14 at 22:57
-
As @PeterGibson said it's NOT work on Windows! just mounts empty folder! – mrgloom Aug 30 '16 at 15:05
*Update - see answer below. this is no longer the correct answer *
You can't mount them, by design, because Docker could no longer guarantee a repeatable execution environment.
However you can:
Import the host's root filesystem and create a new image from it:
tar -C / -c . | docker import - entend/custombase
Import a bootstrap root filesystem, for example the result of running 'debootstrap'. (Note that this is how the official "base" image was created, so you might be better off simply running 'docker pull base')
debootstrap precise ./bootstrap tar -C ./bootstrap -c . | docker import - entend/ubuntubase
Inject the contents of a local directory into a container when running it.
IMAGE=base; SRC=./stuff; DST=/tmp/stuff; CMD="echo hello world"; tar -C $src -c . | docker run $IMAGE -i /bin/sh -c "tar -C $DST -x; $CMD"
This will run a container from $IMAGE, copy host directory $SRC into container directory $DST, then run command $CMD.
This last example is typically used to insert source code before running a build command inside the container.

- 30,738
- 21
- 105
- 131

- 211
- 2
- 4
-
2The syntax for #3 seems to have changed; the `-i` flag needs to come before the image, otherwise docker run will try to find the image called `-i`. – Kevin L. Apr 14 '13 at 18:24
-
When doing approach #3, how can I get the output of `$CMD`? It seems like the -i option suppress output from `docker run`. – Naveed Apr 26 '13 at 08:55
-
2As an update, this is now a first-class option in docker through the ``-v`` option in ``docker run``. More information below. – user2089674 Sep 27 '13 at 19:48
-
2It might be a good idea to change the accepted answer to the one mentioning the new `-v` command line switch instead – Joakim Oct 22 '14 at 20:45
Just to update this question, this will soon be possible in Docker.
This pull request has actually implemented this feature and will be soon merged to master.
You can use it right now if you install this fork.

- 8,315
- 4
- 35
- 39
This IS possible in Docker:
Mount data into application container:
docker run -t -i -rm -volumes-from DATA -name client1 ubuntu bash

- 30,738
- 21
- 105
- 131

- 2,536
- 1
- 18
- 10
Trick for OS X and Windows
Two successive mounts: I guess many posts here might be using two boot2docker's. The reason you don't see anything is because you are mounting a directory from boot2docker, not from your host. You basically need two successive mounts: the first one to mount a directory from your host to your system and the second to mount the new directory from boot2docker to your container like this:
Mount local system on boot2docker:
sudo mount -t vboxsf hostfolder /boot2dockerfolder
Mount boot2docker file on a Linux container
docker run -v /boot2dockerfolder:/root/containerfolder -i -t imagename
Then when you do ls
inside containerfolder you will see the content of your hostfolder
Update July 2021
Dockerfile solution
Add the following line in your Dockerfile
which copies the data from your host machine to the created image (container).
# Other lines of Dockerfile
COPY <relative-or-absolute-path-of-the-directory-in-host-machine> <relative-or-absolute-path-of-the-directory-in-container>
# Example:
# COPY ./redis/data/:/db_data
P.S.: Container is a running image.
Docker-compose solution
Volumes can be defined in various ways in docker-compose file. One of them is to mount the directory address of your host machine to the container. (like what we have in the dockerfile solution) as one part of your service:
volumes:
- <relative-or-absolute-path-of-the-directory-in-host-machine>:<relative-or-absolute-path-of-the-directory-in-container>

- 5,883
- 8
- 64
- 102