Part 1 of Building a Frontend Dashboard: Set Up GridDB on MacOS with Node and Docker

In this tutorial, we’ll be setting up a Node app with GridDB in a Docker container.

By the end of this tutorial, you’ll know how to:

  • Set up a containerized Node and GridDB app with Docker
  • Create an Express server which connects to GridDB

This tutorial will be useful if you’re using GridDB in an environment like MacOS or another Linux OS which doesn’t support GridDB. It’ll also be useful if you want to keep your GridDB app isolated from the rest of your system.

I presume you’ve got Docker already installed on your system and are familiar with how it works. I also presume you understand the basics of Node.js.

You can access the full code from this tutorial in this repo.

Setting up Node app

The first thing we’ll do is set up a simple Node app. Later, we’ll containerize it, and then integrate GridDB.

Inside your project directory, create a package.json file and add this content:

package.json

{
  "name": "griddb-node-docker",
  "version": "1.0.0",
  "description": "",
  "author": "",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  }
}

As you can see, we’ve got a start script that runs a file server.js which will be our main app file.

Let’s now create that file and console log a “Hello, World” message:

server.js

console.log('Hello, World');

To test this works, run npm run start from the terminal and you should see your message printed.

$ npm run start

Express server

Let’s install Express which allows us to create a quick and simple Node server.

$ npm i -S express

We’ll now set up Express in our server.js file. Replace the previous content with:

server.js

const express = require('express');

const PORT = 3000;
const HOST = '0.0.0.0';

const app = express();
app.get('/', (req, res) => {
  res.send('Hello, World');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

What this does:

  • Requires Express package
  • Creates an Express app
  • Creates a handler for the root path which sends the text “Hello, World”
  • Listens on localhost:3000

Let’s now run the Express server:

$ npm run start

To test our Express server is working, let’s use Curl in another terminal:

$ curl http://localhost:3000

You should get the “Hello, World” message back.

Nodemon

If we make changes to our Node/Express app, we need to restart the server manually for the changes to take effect.

This is not ideal when developing with Docker, because we’ll have to keep starting and stopping the Docker app.

A better solution is to use the nodemon package to run our server as this will watch our files and automatically restart the server if anything changes.

Let’s kill our Express server (Ctrl+C) and install Nodemon:

$ npm i -S nodemon

Now, alter our package.json start script so it uses Nodemon.

package.json

{
  ...
  "scripts": {
    "start": "nodemon server.js"
  }
}

Now, if we make any modification to our server.js file the Express server will automatically restart.

Node app container

Now, we’re going to containerize the Node/Express app with Docker. We’re also going to include the Node GridDB connector so that your Node app can talk to GridDB.

Create a new file Dockerfile in your app directory, and include the following:

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 --quiet

ENV LD_LIBRARY_PATH /usr/griddb_c_client-4.3.0/lib/

What this does:

  • Uses the official Node v10 image as a base
  • Downloads and installs the GridDB Node connector
  • Create a directory for the app in the container and copies your package json file there
  • Installs NPM dependencies
  • Puts the GridDB connector location as an environment variable

With that done, we can now build the image:

docker build . -t node-express-griddb

The -t flag allows us to give our image a tag so we can easily find it.

Once the image has finished building run docker images in the terminal and you should see it.

REPOSITORY                TAG       IMAGE ID       CREATED          SIZE
node-express-griddb       latest    8c191df53772   13 seconds ago   954MB

Adding GridDB with Docker Compose

We’ve now got the Node app and GridDB Node connector in an image. To make a working app, we’ll use Docker Compose and add in GridDB.

Go ahead and create a file docker-compose.yml in the app directory, and include the following:

version: "3.8"
services:
  web:
    build:
      context: ./
    volumes:
      - .:/opt/nodejs
            - /opt/nodejs/node_modules
    command: npm run start
    ports:
      - "3000:3000"
    depends_on:
      - griddb
  griddb:
    image: "griddbnet/griddb"

What this does:

  • Creates a service that uses the image we just created
  • Mounts our current directory in the container
  • Exposes port 3000
  • Create a second service from the official GridDB Docker image.

You should also create a file .dockerignore which tells Docker to ignore certain files. Since we’re mounting your app directory, we don’t want to include local environment and project files.

node_modules
npm-debug.log
.git
.idea

Running Docker app

Now that we have our services defined, we can build the application using:

$ docker-compose up

In another terminal well use Curl to confirm the app is working.

$ curl http://localhost:3000

You should again get the “Hello, World” message back. This time, though, the app is running from an isolated Docker environment.

Working with GridDB Node Client

We’ve now got a Dockerized Node and GridDB environment. We can start developing a GridDB-based app now.

First, we’ll need to use the GridDB Node client package, so let’s install that:

$ npm i -S griddb_node

Note: after installing new packages you’ll need to rebuild the image as the NPM dependencies are written to the image. To do this, kill your docker-compose app and run docker-compose up --build to start it again and rebuild the image.

Let’s now create a new file, db.js, where we’ll set up our GridDB instance so it can be used in our Express server.

db.js

const griddb = require('griddb_node');

const connect = () => {
  const factory = griddb.StoreFactory.getInstance();
  return factory.getStore({
    "notificationMember": "griddb:10001",
    "clusterName": "defaultCluster",
    "username": "admin",
    "password": "admin"
  });
};

module.exports = { connect };

This exports a method connect which returns a GridDB store. This will work flawlessly in the Docker environment because the GridDB container exposes port 10001.

Connecting to GridDB in an Express App

We can now require our db module in the main server app. When we call connect, we get back a Store instance which can then be used to read and write GridDB containers.

server.js

const express = require('express');
const db = require('./db');

const store = db.connect();

...

This concludes part one of our blog. In part two, we will actually go in and create a dashboard using this new-found knowledge

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.