Create a Node.js image

Hopefully you’ve read my last post on how to install on a VPS (XEN) that runs Ubuntu. No it’s time to actually use and take advantage of how easy it is to create images to deploy on other servers.

Just for fun, I will create an Ubuntu base image that has node.js and npm installed.

Step 1

Enter the base image and install some dependencies.

#enter a base image
docker run -i -t base /bin/bash

#make sure all packages are up to date
apt-get update && apt-get upgrade

#Install some packages we need
apt-get install python-software-properties python g++ make software-properties-common

#Add node repository to sources.list and update apt
add-apt-repository ppa:chris-lea/node.js && apt-get update

#Install node.js
apt-get install nodejs

#Test it
node -v

Step 2

Lets create a small node app, just to test it.

mkdir /var/www && echo 'console.log("Hello from Node.js");' > /var/www/app.js

#Test it
node /var/www/app.js


Nice, it works!

Step 3

Alright, now we want to save an image of this so we can start an instance os this whenever we want to setup a node server fast.

#Exit the docker image

#List all containers/images
docker ps -a

#Create a new image from your latest edit
docker commit 0a7e9dd8dbdd NodeBase

#List all images to see that it was created
docker images


Step 4

Lets start a container and forward port 80 to it so you can point your web browser to your VPS ip and get served node from the container.

#Start a new container
docker run -i -t -p :80 NodeBase /bin/bash

We will create a very basic web server, here’s the code inserted in app.js below.

var http = require('http');
function requestHandler(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World');

Insert the code above in /var/www/app.js

nano /var/www/app.js

#Start the node server
node /var/www/app.js

Now you should be able to point your browser to the ip of your VPS and get served a page like this.
Up and running

We’re still in the docker container in interactive mode and our web server will only be up as long as we’re attached to the shell. So, lets save this container as a new image and then start everything in daemon mode.

#exit the container

#commit a new image with the above changes. Find the id
docker ps -a

#Insert your container id where it says 'your_id' below.
docker commit your_id NodeHTTP

#Start in daemon mode and invoke node to start the node web server
docker run -d -p :80 NodeHTTP node /var/www/app.js

#Check to see if its active
root@container2:/home/oskar# docker ps
ID                  IMAGE               COMMAND                CREATED              STATUS              PORTS
e821cf59b2c8        NodeHTTP:latest     node /var/www/app.js   About a minute ago   Up About a minute   80->80

Now you can point your browser to the VPS ip again and you should see the same page as before.

You can stop the container by typing docker stop e821cf59b2c8. And start again by docker start e821cf59b2c8. Substitute e821cf59b2c8 with the id of your container.