I am trying to use docker-machine with docker-compose. The file docker-compose.yml has definitions as follows:
web:
build: .
command: ./run_web.sh
volu
It can be done witch combination of three tools:
docker-machine mount
, rsync
, inotifywait
TL;DR
Script based on all below is here
Let's say you have your docker-compose.yml
and run_web.sh
in /home/jdcaballerov/web
docker-machine machine:/home/jdcaballerov/web /tmp/some_random_dir
rsync -r /home/jdcaballerov/web /tmp/some_random_dir
Synchronize on every change of files in your directory:
inotifywait -r -m -e close_write --format '%w%f' /home/jdcaballerov/web | while read CHANGED_FILE
do
rsync /home/jdcaballerov/web /tmp/some_random_dir
done
BE AWARE - there are two directories which has same path - one is on your local (host) machine, second is on docker machine.
I am using docker-machine 0.12.2 with the virtualbox drive on my local machine. I found that there is a directory /hosthome/$(user name)
from where you have access to local files.
Also ran into this issue and it looks like local volumes are not mounted when using docker-machine. A hack solution is to
get the current working directory of the docker-machine instance docker-machine ssh <name> pwd
use a command line tool like rsync
to copy folder to remote system
rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:<result _of_pwd_from_1>.
The default pwd is /root so the command above would be rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:/root
NB: you would need to supply the password for the remote system. You can quickly create one by ssh into the remote system and creating a password.
change the volume mount point in your docker-compose.yml
file from .:/app
to /root/<name_of_folder>:/app
run docker-compose up -d
NB when changes are made locally, don't forget to rerun rsync
to push the changes to the remote system.
Its not perfect but it works. An issue is ongoing https://github.com/docker/machine/issues/179
Other project that attempt to solve this include docker-rsync
All other answers were good for the time but now (Docker Toolbox v18.09.3) all works out of the box. You just need to add a shared folder into VirtualBox VM.
Docker Toolbox automatically adds C:\Users
as shared folder /c/Users
under virtual linux machine (using Virtual Box shared folders feature), so if your docker-compose.yml
file is located somewhere under this path and you mount host machine's directories only under this path - all should work out of the box.
For example:
C:\Users\username\my-project\docker-compose.yml
:
...
volumes:
- .:/app
...
The .
path will be automatically converted to absolute path C:\Users\username\my-project
and then to /c/Users/username/my-project
. And this is exactly how this path is seen from the point of view of linux virtual machine (you can check it: docker-machine ssh
and then ls /c/Users/username/my-project
). So, the final mount will be /c/Users/username/my-project:/app
.
All works transparently for you.
But this doesn't work if your host mount path is not under C:\Users
path. For example, if you put the same docker-compose.yml
under D:\dev\my-project
.
This can be fixed easily though.
docker-machine stop
).Open Virtual Box GUI, open Settings of Virtual Machine named default
, open Shared Folders
section and add the new shared folder:
D:\dev
d/dev
Press OK
twice and close Virtual Box GUI.
docker-machine start
).That's all. All paths of host machine under D:\dev
should work now in docker-compose.yml
mounts.
Since October 2017 there is a new command for docker-machine that does the trick, but make sure there is nothing in the directory before executing it, otherwise it might get lost:
docker-machine mount <machine-name>:<guest-path> <host-path>
Check the docs for more information: https://docs.docker.com/machine/reference/mount/
PR with the change: https://github.com/docker/machine/pull/4018
If you choose the rsync option with docker-machine, you can combine it with the docker-machine ssh <machinename>
command like this:
rsync -rvz --rsh='docker-machine ssh <machinename>' --progress <local_directory_to_sync_to> :<host_directory_to_sync_to>
It uses this command format of rsync, leaving HOST
blank:
rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
(http://linuxcommand.org/man_pages/rsync1.html)