I'm using boot2docker on OS X and cloned the following repo:
https://github.com/enokd/docker-node-hello
It basically has a Dockerfile and a very simple express app that prints hello world. Everything runs great when I build and run the image, but of course if I make any changes to index.js on my Mac none of that gets reflected in my running image. I can't seem to find any references on how I'd setup docker so that I can run it in my development environment to automatically pick up my source code changes so I feel like I'm "Doing it wrong". Any suggestions?
Here's how I'm currently running it (I'm not using Vagrant, and not quite sure if that makes any difference):
$ docker build -t gasi/centos-node-hello .
$ docker run -p 49160:8080 -d gasi/centos-node-hello
$ curl localhost:49160
Update: Added an answer with what I ended up doing.
Update: Added more current answer using boot2docker 1.3+ and fig.
This is what I ended up doing, so far seems to work but I'm still digging into it:
In my project root, I simply run:
Credit goes to this source.
Update for docker 1.3 and fig
Fig is great, it really took a lot of the complexity out of the script I had before. In addition, boot2docker now natively supports mounting volumes on Mac OS X using Virtual Box's shared folders. This is what I find works really well for me now:
First, the
Dockerfile
:The
fig.yml
:Here's the new
bin/run
:I also have a
bin/install
script that does thenode_modules
dependency installs. This assumes I've already done an npm install on my host so that any private packages will work. Also, this works great with npm links, you just need to make a symlink from your/home/linkedProject
into$NODE_PATH/linkedProject
in your container.So, to put this all together, here's the steps in order:
Create my data volume container:
docker run -v $HOME/data:/data:ro \ -v /home \ -v /path/to/NODE_PATH \ --name myvols myNodeImage echo Creating my volumes
Run my install script:
cd ~/data/myProject && ./bin/install
Run my run script:
nodemon -x bin/run
Concerning shared folders with VirtualBox, please check out:
boot2docker together with VirtualBox Guest Additions
How to mount /Users into boot2docker
https://medium.com/boot2docker-lightweight-linux-for-docker/boot2docker-together-with-virtualbox-guest-additions-da1e3ab2465c
EDIT: updated link to boot2docker v1.1.0
There's several scenarios that docker can help on web development:
lighting fast provision all different kinds of services locally. you want a clean couchdb, run
docker run -d -p 5984:5984 tutum/couchdb
; mysql, no problem:docker run -d -p 3306:3306 tutum/mysql
, a selenium server with firefox? easy:sudo docker run -p 4444:4444 -d lzhang/selenium
, you will get them immediately, and they can be destroyed bydocker kill
the next seconds you don't want them. fit the local testing scenario very well, no need to worry how to configure them.manage software dependencies. for example, you want to run your node app under different node versions (0.8, 0.10, 0.10.25, etc), just find the nodejs docker images by
docker search nodejs
, and create containers by mounting your application directory as volume, you can run multiple containers which is isolated to each other. Take a look at my keystonejs-example project on how to run a complicated node app with mongodb in seconds with 0 configuration. thinking a more complicated model: load balancer + app + database + cache, in old school way, there're tons of configuration options to fit the setup procedure, but, if you provision them as separate containers and link them by names, every components can discover each other by local environment variables, just like a local PaaS.an easy way to use application by searching docker index. for example, there is a neat tool called Heartbleed checker, you can quickly download/use it in a container with ready configuration by a single command, even don't need to think about config, download language runtime, configure, uninstall, etc.
Per boot2docker, I assume you're on OSX, so you'd better pickup version v0.9.2+ which supports hostonly network configuration, then you can access the containers run inside the VM over the hostonly network.
Your question is really interesting, and (partially) not specifically related to Docker. Let me say that the main and final problem here is the use of a virtual machine.
Tracking code changes on host computer can be a challenge when you use a VM (bare Virtualbox VM, Vagrant VM, Docker boot2docker VM...)
The VM is a complete layer of abstraction between your Mac (where your code changes happen) and your application stack (here, the Docker container running on a tiny Linux VM).
You have to explicitly define a shared folder (NFS, ...) in Virtualbox, between your Mac and boot2docker. This shared folder would be the root of your code repository. That's the first step.
Mac: /Users/You/stuff/approot ===> boot2docker: /something
Having NodeJs detecting files change is another challenge. You need an additional wrapper watching the filesystem and restarting Node upon code changes (Forever, Nodemon...).
And, after that since the changes don't occur on a local filesystem, but on a shared folder, you'll probably have to tell the Nodejs watcher (Forever/Nodemon/...) to use polling mode. It works (more or less) but will burn a lot of CPU.