I am trying to mount a host directory into a Docker container so that any updates done on the host is reflected into the Docker containers. I couldn't succeed after googling and reading the Docker volume links.
Where am I doing something wrong. Here is what I did:
kishore$ cat Dockerfile
FROM ubuntu:trusty
RUN apt-get update
RUN apt-get -y install git curl vim
CMD ["/bin/bash"]
WORKDIR /test_container
VOLUME ["/test_container"]
kishore$ tree
.
├── Dockerfile
└── main_folder
├── tfile1.txt
├── tfile2.txt
├── tfile3.txt
└── tfile4.txt
1 directory, 5 files
kishore$ pwd
/Users/kishore/tdock
kishore$ docker build --tag=k3_s3:latest .
Uploading context 7.168 kB
Uploading context
Step 0 : FROM ubuntu:trusty
---> 99ec81b80c55
Step 1 : RUN apt-get update
---> Using cache
---> 1c7282005040
Step 2 : RUN apt-get -y install git curl vim
---> Using cache
---> aed48634e300
Step 3 : CMD ["/bin/bash"]
---> Running in d081b576878d
---> 65db8df48595
Step 4 : WORKDIR /test_container
---> Running in 5b8d2ccd719d
---> 250369b30e1f
Step 5 : VOLUME ["/test_container"]
---> Running in 72ca332d9809
---> 163deb2b1bc5
Successfully built 163deb2b1bc5
Removing intermediate container b8bfcb071441
Removing intermediate container d081b576878d
Removing intermediate container 5b8d2ccd719d
Removing intermediate container 72ca332d9809
kishore$ docker run -d -v /Users/kishore/main_folder:/test_container k3_s3:latest
c9f9a7e09c54ee1c2cc966f15c963b4af320b5203b8c46689033c1ab8872a0ea
kishore$ docker run -i -t k3_s3:latest /bin/bash
root@0f17e2313a46:/test_container# ls -al
total 8
drwx------ 2 root root 4096 Apr 29 05:15 .
drwxr-xr-x 66 root root 4096 Apr 29 05:15 ..
root@0f17e2313a46:/test_container# exit
exit
kishore$ docker -v
Docker version 0.9.1, build 867b2a9
- I don't know how to check boot2docker version
Questions, issues facing:
- How do I need to link the main_folder to the test_container folder present inside the docker container?
- I need to make this automatically. How do I to do that without really using the
run -d -v
command? - What happens if the boot2docker crashes? Where are the Docker files stored (apart from Dockerfile)?
I've been having the same issue. My command line looked like this:
The problem was with 'pwd'. So I changed that to $(pwd):
For Windows 10 users, it is important to have the mount point inside the
C:/Users/
directory. I tried for hours to get this to work. This post helped but it was not obvious at first as the solution for Windows 10 is a comment to an accepted answer. This is how I did it:Then to test it, you can do
echo TEST > hostTest.txt
inside your image. You should be able to see this new file in the local host folder atC:/Users/C/Desktop/dockerStorage/
.I found that any directory laying under system directive like
/var
,/usr
,/etc
could not be mount under the container.The directive should be at user's space
-v
switch instructs docker daemon to mount local directory to the container, for example:How do I link the main_folder to the test_container folder present inside the docker container?
Your command below is correct, unless your on a mac using boot2docker(depending on future updates) in which case you may find the folder empty. See mattes answer for a tutorial on correcting this.
I need to make this run automatically, how to do that without really using the run -d -v command.
You can't really get away from using these commands, they are intrinsic to the way docker works. You would be best off putting them into a shell script to save you writing them out repeatedly.
What happens if boot2docker crashes? Where are the docker files stored?
If you manage to use the -v arg and reference your host machine then the files will be safe on your host.
If you've used 'docker build -t myimage .' with a Dockerfile then your files will be baked into the image.
Your docker images, i believe, are stored in the boot2docker-vm. I found this out when my images disappeared when i delete the vm from VirtualBox. (Note, i don't know how Virtualbox works, so the images might be still hidden somewhere else, just not visible to docker).
2 successive mounts : I guess many posts here might be using two boot2docker , the reason you don't see anything is because you are mounting a directory from boot2docker not from your host. You basically need 2 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
2. mount boot2docker file on linux container
then when you
ls
inside containerfolder you will see the content of your hostfolder[UPDATE] As of ~June 2017, Docker for Mac takes care of all the annoying parts of this where you have to mess with VirtualBox. It lets you map basically everything on your local host using the
/private
prefix. More info here. [/UPDATE]All the current answers talk about Boot2docker. Since that's now deprecated in favor of docker-machine, this works for docker-machine:
First, ssh into the docker-machine vm and create the folder we'll be mapping to:
Now share the folder to VirtualBox:
Finally, ssh into the docker-machine again and mount the folder we just shared:
Note: for UID and GID you can basically use whatever integers as long as they're not already taken.
This is tested as of docker-machine 0.4.1 and docker 1.8.3 on OS X El Capitan.