Containerizing your Own Node.js Application

Node.js Docker Container Image

We recently put out a docker blog which helped guide users to using containerized versions of both a GridDB server and then another container which ran an application. In that original blog (and also video), we detailed using the containerized application with a language-specific interface.

As a simple launching point, we made public docker images for a GridDB server, a Python application, and a Java application, all of which are available on Docker Hub. And now, as promised, we’ve added a Node.js container image to go along with our previous two. Just as with our other images, this will allow you to run your node.js griddb application in a very-portable container.

This blog will very quickly go over how to use the newly made image and how to insert your own code once you confirm that the containerized application works well in your environment.

Getting Started

First, let’s take a look at how the Node.js image looks like:

FROM node:10

RUN wget https://github.com/griddb/c_client/releases/download/v4.3.0/griddb-c-client_4.3.0_amd64.deb
RUN dpkg -i griddb-c-client_4.3.0_amd64.deb

WORKDIR /opt/nodejs
COPY package*.json ./
RUN npm install

ENV LD_LIBRARY_PATH /usr/griddb_c_client-4.3.0/lib/

ADD blogSample.js /opt/nodejs
CMD ["node", "/opt/nodejs/blogSample.js"]

The container itself is very simple now that the GridDB Node.js client is available via npm. As you can see, the only real requirements for the client (inside the container) are to use version 10 of Node.js, and to install the GridDB c_client. Once it’s installed, we simply copy over a package.json file which lists the node.js client as a dependent and then runs “npm install”. When attempting to get up running yourself, you will simply create your own Dockerfile to copy over your own nodejs code in place of our blogSample.js file.

One small note: if you are running the containerized version of the GridDB server, the method of connecting looks like the following:

const factory = griddb.StoreFactory.getInstance();
const store = factory.getStore({
    "notificationMember": "griddb-server:10001",
    "clusterName": "defaultCluster",
    "username": "admin",
    "password": "admin"
});

Of course, if you are running just a normal server, the connection information should remain the same.

Running

And then actually running is the same as any normal Docker container:

[root@GridDB nodejs]$ docker build -t griddb-nodejs .
Sending build context to Docker daemon  2.114MB
Step 1/9 : FROM node:10
 ---> d9b29c7fd8c0
Step 2/9 : RUN wget https://github.com/griddb/c_client/releases/download/v4.3.0/griddb-c-client_4.3.0_amd64.deb
 ---> Using cache
 ---> 6550ce141d57
Step 3/9 : RUN dpkg -i griddb-c-client_4.3.0_amd64.deb
 ---> Using cache
 ---> ba30fe035674
Step 4/9 : WORKDIR /opt/nodejs
 ---> Using cache
 ---> 1e18971d3670
Step 5/9 : COPY package*.json ./
 ---> Using cache
 ---> 372cbda5a26c
Step 6/9 : RUN npm install
 ---> Using cache
 ---> 996f7d0099c9
Step 7/9 : ENV LD_LIBRARY_PATH /usr/griddb_c_client-4.3.0/lib/
 ---> Using cache
 ---> 3c35b0f512c8
Step 8/9 : ADD blogSample.js /opt/nodejs
 ---> Using cache
 ---> 254f4b957808
Step 9/9 : CMD ["node", "/opt/nodejs/blogSample.js"]
 ---> Using cache
 ---> 9184cfcd24dc
Successfully built 9184cfcd24dc
Successfully tagged griddb-nodejs:latest

And then simply run it while including the option for your docker network which is shared with your GridDB server (unless you’re running with a local GridDB server instead of a containerized version).

$ docker run --network griddb-net -t griddb-nodejs

While that’s essentially all there is to it, I would like to share one command from the previous blog. This command will show you the IP address of your containerized GridDB-server, just in case you would like to run your server in a container, but omit running your application in one:

$ CONT=`docker ps | grep griddb-server | awk '{ print $1 }'`; docker exec $CONT cat /etc/hosts | grep $CONT | awk '{ print $1 }'

If you have any lingering questions, please feel free to contact us.

If you have any questions about the blog, please create a Stack Overflow post here https://stackoverflow.com/questions/ask?tags=griddb .
Make sure that you use the “griddb” tag so our engineers can quickly reply to your questions.