Docker for development

After coming across being convinced by this article that Docker is a useful tool for running a dev environemnt, I’ve been running my recent Node.js-based FitBit logger project from a Docker container. I’m looking at Amazon’s EC2 Container Service to host the thing, so I’m writing basic Docker rather than something fancier like Compose.

Setting up a Node.js container

This tutorial on the docs.docker.com site goes through the process of building a Docker image for Node.js. Here is the one I ended up with by following their instructions:

FROM    centos:6

RUN     rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
RUN     yum install -y npm

# App
ADD . /src
WORKDIR /src
RUN npm install

# For Dev
RUN npm install -g nodemon

EXPOSE  80
CMD ["node", "/src/server.js"]

Note the install -g nodemon: Nodemon will monitor your filesystem for changes and restart a Node.js server when source code is modified.

This is how I start up my container using Nodemon:

docker run -d -p 80:80 -v /Users/miller/dev/fitbit-logger:/src --name fbit millerpeterson/fitbit-logger nodemon server.js

One thing to watch is that you have to have run npm install at least once locally, so that the node_modules directory is created. You need this because the contents of the local source directory will get copied over the container source directory with the -v /Users/miller/dev/fitbit-logger:/src portion of the command.

Now you can edit your code and have it reflected in your Docker container automatically, without having to run additional commands on the container!