Run your Node.js app in Docker container

Overview

In this post we will create a Docker container for Node.js. Then we can deploy it very easily to production.

Node.js microservice

For that example we will create a microservice which displays hostname of the application.

1
2
3
4
5
6
7
8
9
10
var http = require('http')
var os = require('os')

var port = process.argv[2]

var server = http.createServer(function (req, resp) {
	resp.writeHead(200, {'Content-Type': 'text/plain'})
  resp.end('I am: ' + os.hostname() + '\n')
}).listen(port)
console.log('Server running at http://127.0.0.1:' + port)

Dockerfile

We will use nodejs image provided by the Dockerfile group.

1
2
3
4
5
6
7
8
9
FROM dockerfile/nodejs

ADD app.js /myapp/app.js

EXPOSE 1337
WORKDIR "/myapp"

ENTRYPOINT ["node", "app.js", "1337"]
CMD []

Building and running

  • Build image:

docker build -t myapp .

  • Run container:

docker run -d -p 1337:1337 --name myapp