Today, I am taking a request from one of our fans on Twitter.

Twitter screen shot

Apparently, I have talked too much about building WordPress in Docker, so today, I am going to do something totally different. Today we are building a simple Node app with a web and worker.

Simple Docker Environment

First, let's start by setting up our Docker environment if we don't have one already.

$ brew install boot2docker node # on OS X
$ boot2docker init && boot2docker up # on OS X
$ sudo gem install bundler highline building

Simple Node.js Web App

Now let's create a simple Node web app called web.js.

// web.js
var express = require("express");
var logfmt = require("logfmt");
var app = express();


app.get('/', function(req, res) {
  res.send('Hello World!');

var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
  console.log("Listening on " + port);

So far so good. Let's create a simple package.json file.

$ npm init

And let's install the dependencies.

$ npm install express logfmt --save

Our web app is ready to be Dockerized. We are just one command away:

$ building myuser/nodeapp
creating Dockerfile
building docker build -t myuser/nodeapp:latest .
Uploading context 1.962 MB
Uploading context
Step 0 : FROM ctlc/buildstep:ubuntu13.10
---> a5432f93c775
Step 1 : ADD . /app
---> be7dc25225b4
Step 2 : RUN /build/builder
---> Running in 2e6d9222a739
Node.js app detected
-----> Defaulting to latest stable node: 0.10.26
-----> Downloading and installing node
-----> Found existing node_modules directory; skipping cache
-----> Rebuilding any native dependencies
-----> Writing a custom .npmrc to circumvent npm bugs
-----> Installing dependencies
-----> Caching node_modules directory for future builds
-----> Cleaning up node-gyp and npm artifacts
-----> Building runtime environment
-----> Discovering process types
---> c10e439c315b Step 3 : CMD /start web
---> Running in fd2ffd305e36
---> f0f1d0fcae55
Successfully built f0f1d0fcae55
hint To run your app, try: docker run -d -p 8080 -e "PORT=8080"     
hint To re-build your app, try: docker build -t myuser/nodeapp .

The Docker container is now built. We can test this by running:

$ docker run -i -t -p 8080 -e "PORT=8080" myuser/nodeapp:latest /start web
Listening on 8080

Simple Background Processes in Docker

So now how do you create a background process? Easy. First let's create the background process file. Let's call it background.js.

// background.js
var sleep = require('sleep');

while (true) {
  console.log("Doing background stuff...");

Since we are adding the sleep library, let's install the dependency.

$ npm install sleep --save

Now let's create a new file called Procfile. There is a lot you can learn about Procfiles, but for now we will keep it simple.

web: node web.js
worker: node background.js

One line for the frontend web app and one for the backend worker app. Since the building tool already created a Dockerfile for us, let's just use docker build to update our container.

$ docker build -t myuser/nodeapp .
Uploading context 1.962 MB
Uploading context
Step 0 : FROM ctlc/buildstep:ubuntu13.10
---> a5432f93c775 Step 1 : ADD . /app
---> Using cache ---> be7dc25225b4
Step 2 : RUN /build/builder
---> Using cache
---> c10e439c315b
Step 3 : CMD /start web
---> Using cache
---> f0f1d0fcae55 Successfully built f0f1d0fcae55

And now we can run our background worker by changing the word "web" to worker" in the docker run command.

$ docker run -i -t myuser/nodeapp:latest /start worker
Doing background stuff...
Doing background stuff...
Doing background stuff...

If you want to run the container in the background, use docker run -d instead of docker run -i -t

$ docker run -d myuser/nodeapp:latest /start worker
$ docker ps
bf44762d62d1 myuser/nodeapp:latest /start worker 1 seconds ago Up 1 seconds     elegant_fermat


Background tasks are easy to build in Docker. In fact, Docker is perfectly suited for them. Whether you are building a Java Tomcat app that needs a background task or any other kind of app, you can combine the genius of Docker with the simplicity of Heroku Procfiles to accomplish wonderful results.

The original request was to show how background tasks could talk to a database backend, but since a worker container and a web container are the same in the eyes of Docker, you can just use any tutorial about connecting containers to databases to accomplish the task.

Do you have a request for a Docker tutorial? Tweet us at @CenturyLinkLabs and let us know what you would like to see.