How to Install Hapi.js Inside Docker Container

Cloud Native is a term created by the devops community to describe the ecosystem to run containers in a distributed environment.  In this tutorial we will install Hapi.js inside a docker container that can be used in a Cloud Native environment.

Installing Hapi.js

Prerequisite: Homebrew on Mac or the Windows equivalent.

For this part we going to be using Node.js and NPM on our local machine. We will need Homebrew installed on our Mac or the Windows equivalent.

First, create a directory for your hapi-backend-container and change directory into it:

mkdir hapi-backend-container; cd hapi-backend-container

Create a package.json file by typing ‘npm init’ and filling in the prompts:

npm init

Its most common for Node.js applications to have server.js be its entry point. For the entry point prompt, put server.js instead index.js when doing npm init.

Install hapi and nodemon:

npm install --save hapi
npm install --save nodemon

Now open up package.json in your text editor and add a start command right above the test command inside the script block:

...
  "scripts": {
    "start": "nodemon server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
...

Our amazing web application will be started by copying and pasting this from the hapi.js getting started page into a file called server.js:

'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({ port: 3000, host: '0.0.0.0' });

server.route({
 method: 'GET',
 path: '/',
 handler: function (request, reply) {
 reply('Hello, world!');
 }
});

server.route({
 method: 'GET',
 path: '/{name}',
 handler: function (request, reply) {
 reply('Hello, ' + encodeURIComponent(request.params.name) + '!');
 }
});

server.start((err) => {

if (err) {
 throw err;
 }
 console.log(`Server running at: ${server.info.uri}`);
});

Be sure that the host is ‘0.0.0.0’.

Now run the application by typing:

npm start

Go to localhost:3000 to see the words “Hello World”.

To “Dockerize” your app you’ll need to create an image of the app. Do this by creating a Dockerfile:

FROM node:alpine

MAINTAINER <username@emailaddress.com>

# Install bash
RUN apk add --update bash && rm -rf /var/cache/apk/*

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 3000
CMD [ "npm", "start" ]

Now we can build image by running this command:

docker build -t <yourusername>/hapi-backend-container .

NPM will take a few moments to download its packages and dependencies.

Once its done, you can run the Docker image by typing:

docker run -p 3000:3000 --name hapi-backend-container -d <yourusername>/hapi-backend-container

Stop the Docker container by using the name we give it, or the name or id given by Docker. We can see the name and ID by typing:

docker ps

We use the -f flag to force the removal of a running container:

docker rm hapi-backend-container -f

To run the image with a shared volume for developing on your local computer:

docker run -p 3000:3000 -v "$PWD"/:/usr/src/app --name hapi-backend-container -d <yourusername>/hapi-backend-container

To run linked to our previously created ‘mongo-db-container’:

docker run --link mongo-db-container:mongo -p 3000:3000 -v "$PWD"/:/usr/src/app --name hapi-backend-container -d <yourusername>/hapi-backend-container

Stop the container:

docker stop hapi-backend-container

Create a docker-compose.yml file and put this into its contents:

version: '3'
services:
 hapi-backend-container:
 container_name: hapi-backend-container
 image: <yourusername>/hapi-backend-container
 build: .
 ports:
 - "3000:3000"
 volumes:
 - .:/usr/src/app

Now you can run the container with the settings from the docker-compose.yml by typing:

docker-compose up -d

Congratulations! You now have a Hapi.js backend connected to a mongoDB!

Helpful Commands:

docker exec -it hapi-backend-container /bin/bash