Keep your project in sync with your docker-machine host

Posted on Tue 05 July 2016 in docker

Docker-machine is a great tool to create remote instances to run your containers on. But, as of today, it does not provide any way to keep your project code in sync with your remote host. For example, this become a problem when you start mounting configuration files as volumes in your containers. Of course you could run a docker-machine scp -r . <machine> each time you modify something, but this solution is rapidly cumbersome and it is not possible to exclude files. The one-liner I'll present in this post will help your solve those issues.

Oh! And there is an open issue to discuss this question.

First thing first install fswatch:

On a mac with:

brew install fswatch

On most linux distribution you should be good to go by installing the inotify-tools package.

The fswatch tool will output events when files are modified in your project. By simply triggering a rsync on such events you can keep your local directy in sync with the remote one. Adapt the different paths to match your directory structure and run a command similar to this one:

fswatch -o -e '.git' . | while read num ; \
do \
    rsync -avzh -e "ssh -i /path/to/home/.docker/machine/machines/machine-name/id_rsa" --progress --exclude '.git' . ubuntu@$(docker-machine ip machine-name):/home/ubuntu; \
done

Note that I filtered out the .git directory here.

I hope this will save you a few headaches and increase your productivity as it did to mine :-)