Pages

Friday, June 6, 2014

Running Node.js in Docker

0. Inform

This is basically a copy of the Hello World example found here and here with some additional comments.

1. Make the VM running

boot2docker up

2. Build the Docker image

Create your Node.js application index.js file:
var express = require('express');
var DEFAULT_PORT = 8080;
var PORT = process.env.PORT || DEFAULT_PORT;
var app = express();
app.get('/', function (req, res) {
  res.send('Hello World\n');
});
app.listen(PORT)
console.log('Running on http://localhost:' + PORT);
Create the Docker package.json file:
{
  "name": "docker-centos-hello-world",
  "private": true,
  "version": "0.0.1",
  "description": "Node.js Hello World app on CentOS using docker",
  "author": "Daniel Gasienica",
  "dependencies": {
    "express": "3.2.4"
  }
}
Create the Dockerfile:
FROM centos:6.4
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
RUN yum install -y npm
ADD . /src
RUN cd /src; npm install
EXPOSE 8080
CMD ["node", "/src/index.js"]
The layout of your directory should be as follows:
> ls
Dockerfile   index.js   package.json
Building the image:
docker build -t peter/centos-node-hello .
Verify if the image exists:
docker images
This should print:
REPOSITORY                TAG                   IMAGE ID            CREATED             VIRTUAL SIZE
peter/centos-node-hello   latest                6c55823e6868        4 hours ago         672.2 MB

3. Running

docker run -p 49160:8080 -d peter/centos-node-hello
The option -p exposes the port 8080 to 49160. This is essential. Without that, the server is not visible to a client.

Verify, if the process is running:
docker ps
You should see:
CONTAINER ID        IMAGE                            COMMAND              CREATED             STATUS              PORTS                     NAMES
578bd01b0407        peter/centos-node-hello:latest   node /src/index.js   4 hours ago         Up 2 hours          0.0.0.0:49160->8080/tcp   jovial_feynman  
Inspect the image:
docker inspect 578bd01b0407
You should see a JSON output showing all relevant information:
...
"PortBindings": {
    "8080/tcp": [
        {
            "HostIp": "0.0.0.0",
            "HostPort": "49160"
        }
    ]
}
...
If the -p was not given when starting the process, HostPort would have been null and the application would not have been visible to clients. Test it:
curl localhost:49160
Now you should see:
Hello World.
Stop and start the process again:
docker stop 578bd01b0407
docker ps doesn't show the process any more. docker ps -a lists the process as Exited:
> docker ps -a
CONTAINER ID        IMAGE                            COMMAND              CREATED             STATUS                       PORTS               NAMES
578bd01b0407        peter/centos-node-hello:latest   node /src/index.js   12 minutes ago      Exited (143) 9 seconds ago                       jovial_feynman     
So, start it again:
docker start 578bd01b0407
Test if the process is running:
> docker -ps
CONTAINER ID        IMAGE                            COMMAND              CREATED             STATUS              PORTS                     NAMES
578bd01b0407        peter/centos-node-hello:latest   node /src/index.js   14 minutes ago      Up 2 seconds        0.0.0.0:49160->8080/tcp   jovial_feynman 
Check the logs:
> docker logs 578bd01b0407
Running on http://localhost:8080
Running on http://localhost:8080

No comments:

Post a Comment