2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2020

04/24/2015: Running the NodeJS Example Inside Docker Container

Yesterday, I showed how to run NodeJS inside a Docker container. Today, I updated my Github project (https://github.com/medined/docker-nodejs) so that the Example server works correctly.

The trick is for the NodeJS code inside the container to find the container's IP address and listen on that address instead of localhost or 127.0.0.1. This is not difficult.



 require('dns').lookup(require('os').hostname(), function (err, add, fam) {
  var http = require('http');
  http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  }).listen(1337, add);
  console.log('Server running at http://' + add + ':1337/');
})

If you're using my Docker image, then you'd just run the following to start the server. Use ^C to stop the server.



node example.js


Now you can browse from the host computer using the following URL. Note that the 'docker run' command exposes port 1337.



http://localhost:1337/


04/23/2015: Running NodeJS (and related tools) from a Docker container.

In my continuing quest to run my development tools from within Docker containers, I looked at Node today.

The Github project is at https://github.com/medined/docker-nodejs.

My Dockerfile is fairly simple:


FROM ubuntu:14.04

RUN apt-get -qq update \
  && apt-get install -y curl \
  && curl -sL https://deb.nodesource.com/setup | sudo bash - \
  && apt-get install -y nodejs \
  && npm install -g inherits bower grunt grunt-cli

RUN useradd -ms /bin/bash developer

USER developer
WORKDIR /home/developer


It's built using:



docker build -t medined/nodejs .



Using the 'developer' user is important because bower can't be used by root. By itself, this container does not look impressive. Some magic is added by the following shell script called 'node':



#!/bin/bash

CMD=$(basename $0)

docker run \
  -it \
  --rm \
  -p 1337:1337 \
  -v "$PWD":/home/developer/source \
  -w /home/developer/source \
  medined/nodejs \
  $CMD $@

I expose port 1337 because that's the port used on the NodeJS home page example. The current directory is exposed in the container at a convenient location. That location is used at the working directory.

You might be puzzled at the use of $CMD. I symlink this script to bower, grunt, and npm. The $CMD invokes the proper command inside the container.