Create and Run Docker Container with Node.js and Node Oracle DB Driver from Windows using Vagrant and VirtualBox image 8

Create and Run Docker Container with Node.js and Node Oracle DB Driver from Windows using Vagrant and VirtualBox

My objective: be able to very quickly run Node.js applications that can connect to an Oracle Database – with minimal disturbance of my laptop environment (Windows 7).

The solution discussed in this article: use Vagrant on Windows – together with VirtualBox – to create a Linux Host VM, install Docker in that VM and provision a Docker Container with Oracle Linux 7, Node.js and the node-oracle package (the Oracle Database Driver) with the Oracle Instant Client. The end result: I can run any Node.js application [with interaction with an Oracle Database] that is located on my local (Windows) file system from the command line. (when I do that, vagrant runs the required VM and Docker Container and connects me to it. The next figure describes that situation.

image

Most of the work I had to do was already done for me by Jaap Poot in this excellent article: Create an oracledb enabled Node.js application container. My only addition was the use of Vagrant for creating and running the Linux Host with Docker inside and orchestrating the starting and stopping of the Docker container. For most details, I will refer to Jaap’s article.

The steps I went through:

  • install Vagrant and VirtualBox on my Windows machine
  • download the Oracle Instant Client packages (as described in Jaap’s article)
  • reuse a Vagrant file to describe the Host VM – the one that will run the Docker engine (read for details this article on how to get started with Docker on Windows using Vagrant)
  • create a Vagrant file to describe the Docker Container that will run Node.js with node-oracledb and the associated Docker file that prescribes the creation of the Docker image underlying that container; ensure that the Oracle Instant Client files are in the same directory as the Docker build file
  • run vagrant up for the second Vagrant file; this starts the dockerhost VM, runs the Docker build process on that VM and produces the container image with Linux, Node.js, Oracle Instant Client and node-oracledb
  • run the Docker container from within the dockerhost and install wget and git; clone the examples repository for node-oracledb; configure the examples for a database instance I have available
  • exit the container and commit the container as a new image: lj/node-oracledb
  • create a 2nd Vagrant file for running a Docker container called node-oracledb-lj-container based on the image created in the previous step; in this Vagrant file – I can define file system mappings into the Docker container as well as port forwarding of networking ports
  • now I have reached my objective: with minimal effort and in very little time, I can run an environment in which Node.js application can easily interact with Oracle Database instances, locally or remotely

 

The remainder of this article will show some of the details on how these steps were taken.

The Docker file for building the Docker container with Node.js and node-oracledb is taken straight from Jaap’s article:

SNAGHTML5e0d3b1

The corresponding Vagrant file that I use to launch the Docker container based on Jaap’s Dockerfile is pretty straightforward:

SNAGHTML5e1e270

I open a command line in Windows in the directory node-oracledb-jaap that contains this Vagrant file and the directory docker with the Dockerfile. Using vagrant up, I kick off the Vagrant process of provisioning the dockerhost VM and the Docker container according to the build instructions in the Dockerfile.

image

This process takes a few minutes, and runs completely unobserved. The container is created and immediately stopped.

image

I open an SSH session into the dockerhost VM. Here I inspect the Docker containers using docker ps -a. This reveals the container id for the container that was just created.

With docker run -i -t 184d /bin/bash (where 184d is the abbreviated container image id), I run the container and open a bash shell as root. Time for some final refinements and checks- before turning the container into an image.

SNAGHTML605b9bc

1. install git using: yum install git -y
2. install wget using: yum install wget -y
3. clone node-oracledb git repository using:  git clone https://github.com/oracle/node-oracledb
image
4. set database connection details in examples/dbconfig.js
5. try out connectivity, node and node-oracle: node examples/select1.js
image

After installing wget and git and cloning the node-oracledb repository on GitHub – and configuring the dbconfig.js file with database connection details to my DBaaS instance and trying out a few of the examples – I exit the container. It is now ready to turn it into an image that I can reuse many times.

With docker ps -a I find the container identifier. Using that identifier in the docker commit <container id> lj/node-oracledb instruction, I easily save the state of the container as a reusable image.

image

 

At this point I exit the SSH terminal on the dockerhost and return to the Windows command line.

Next, I create a new directory – node-oracledb-lj – with a new vagrant-file that allows we to easily run a Docker container based on the lj/node-oracledb image inside the dockerhost VM. I can easily define folder mappings and port forwarding (from Windows host to Docker container) inside this Vagrant file.

SNAGHTML5df1774

 

The final file system set up looks like this:

image

Of these, I do not really need directory node-oracledb-jaap with the Instant Clients any longer – these were used during the construction of the Docker image lj/node-oracledb.

A command line open in the directory node-oracledb-lj can open a terminal inside the Docker Container – with access to Node.js in combination with node-oracledb – with a simple statement such as:

vagrant docker-run -t — bash

A Node.js application can be run from the Windows command line as well – with the application residing either on a [mapped] Windows host directory or inside the Docker container. An example:

vagrant docker-run -t — node /home/nodejs/node-oracledb/examples/select1.js

image

Here the command line runs the Docker container described by the local vagrant file and then runs the statement node /home/nodejs/node-oracledb/examples/select1.js.

Resources

Download zip-file with salient files (Vagrant files, Docker files): vagrant-docker-nodejs-nodeoracledb.zip.

2 Comments

  1. Klaas Maijer April 23, 2016
    • Lucas Jellema April 24, 2016