Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mounting directory from host machine to container in Docker

People also ask

How do I mount a local directory into a container?

How to Mount Local Directories using docker run -v. Using the parameter -v allows you to bind a local directory. -v or --volume allows you to mount local directories and files to your container. For example, you can start a MySQL database and mount the data directory to store the actual data in your mounted directory.

What is the syntax to mount a directory from the Docker host into a directory in the container?

The -v flag is used to mount a host folder, and it consists of two fields separated by a colon. The first part is the path in the host machine. The second part is the path in the container. The --name flag (optional) is used to give the container a specific name.


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/


*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:

  1. Import the host's root filesystem and create a new image from it:

    tar -C / -c . | docker import - entend/custombase
    
  2. 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
    
  3. 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.


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.


This IS possible in Docker:

Mount data into application container:

docker run -t -i -rm -volumes-from DATA -name client1 ubuntu bash

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:

  1. Mount local system on boot2docker:

     sudo mount -t vboxsf hostfolder /boot2dockerfolder
    
  2. 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