A week with Docker: containers for a Node app
I put our Node API at the startup in a Docker container, with MongoDB next to it via Fig. A practical first look, with the Dockerfile, the pitfalls and my tentative opinion.
Everybody has been talking about Docker since the summer. Version 1.0 came out in June, we’re at 1.3 by now, and at every meetup there’s someone who tells you with shiny eyes that “it works on my machine” is finally a thing of the past.
I was sceptical. At the startup we have a working setup: Ubuntu servers, a few shell scripts, Node processes under Upstart, and a README that tells you which MongoDB version to install locally. Does it work well? No. Does it work? Yes.
But our new colleague Bram spent two days setting up his laptop, and Sarah thought that was long enough to give me a week of “playtime”. This is what I learned.
The Dockerfile#
I started from a plain Ubuntu 14.04, because our servers run Ubuntu too and I wanted to change as little as possible at once.
FROM ubuntu:14.04
MAINTAINER Domien <[email protected]>
RUN apt-get update && \
apt-get install -y nodejs nodejs-legacy npm git && \
apt-get clean
# add package.json on its own first, so the npm install layer stays cached
ADD package.json /app/package.json
RUN cd /app && npm install --production
ADD . /app
WORKDIR /app
ENV NODE_ENV production
EXPOSE 3000
CMD ["node", "server.js"]
A few things I learned along the way:
nodejs-legacy: Ubuntu installs Node as/usr/bin/nodejs, because the namenodewas already taken by another package. Lots of npm modules expectnode. Thenodejs-legacypackage adds the symlink. I was stuck on this for an hour.- Layer order: every instruction is a layer, and Docker caches it. If you do
ADD . /appstraight away,npm installruns again on every change to any file. Addingpackage.jsonseparately first saves minutes per build. .dockerignore: exists since version 1.1. Without it I was sending my localnode_modulesand the whole.gitfolder along as build context. 180 MB for a “Hello world” is not a great start.
node_modules
.git
*.log
Build and run:
docker build -t startup/api .
docker run -d -p 3000:3000 --name api startup/api
docker ps
docker logs -f api
MongoDB next to it#
An API without a database isn’t terribly useful. Docker itself lets you connect containers with --link:
docker run -d --name mongo mongo:2.6
docker run -d -p 3000:3000 --link mongo:mongo --name api startup/api
Inside the api container you then get environment variables like MONGO_PORT_27017_TCP_ADDR and MONGO_PORT_27017_TCP_PORT, plus an /etc/hosts entry for mongo. Our config picks that up like this:
var mongoHost = process.env.MONGO_PORT_27017_TCP_ADDR || 'localhost';
var mongoPort = process.env.MONGO_PORT_27017_TCP_PORT || 27017;
var mongoUrl = 'mongodb://' + mongoHost + ':' + mongoPort + '/startup';
It works, but remembering two long docker run commands in the right order is exactly the kind of thing I wanted to get rid of.
Fig#
That’s what Fig is for, which hit 1.0 last month. One YAML file describes your containers:
web:
build: .
ports:
- "3000:3000"
links:
- mongo
environment:
NODE_ENV: development
mongo:
image: mongo:2.6
And then:
fig up
That was the moment I was convinced. Bram tried it on his laptop on Friday. From git clone to a running API with a database: eleven minutes, nine of which were downloading.
The less fun parts#
To be fair:
- On the Mac everything runs through boot2docker, a small Linux VM in VirtualBox. Shared folders for live code reload are slow and sometimes just broken. I’m on Linux, so it didn’t matter to me, but Lotte, our designer, was less enthusiastic.
- Images are big. Mine is over 400 MB. For an API of a few thousand lines of JavaScript.
- Logs and debugging. Since 1.3 there’s
docker exec, which lets you open a shell in a running container (docker exec -it api bash). Before that it wasnsenterand a lot of swearing. It still feels unfamiliar. - Production? How do you do zero-downtime deploys, how do you monitor this, what happens when a host dies? I don’t have a good answer yet, and I don’t think the rest of the world really does either. Jef from the agency would say I shouldn’t roll out anything on a Friday that I don’t understand. He’s right.
Tentative conclusion#
For local development and onboarding new colleagues this is a clear win, and that’s what we’re going to use it for from now on. Production stays as it is for the moment. I first want to see how this whole ecosystem develops over the next year, because it’s moving at a pace that makes me nervous.
Segfault thought the week was a success too, by the way. The laptop was running builds constantly, so it was constantly warm.