In my previous article, First setup of a connection from Node.js to an Oracle Database, I demonstrated how to make a connection to a remote database using Node.js and the node-oracledb module. I used a dedicated VM with Linux 7 installed and Oracle Instantclient provided the 12.1 client libraries.
Now it’s time to take it a step further.
Let’s create an application container and just start it multiple time running any .js script and be able to connect to an Oracle database.
I will start by demonstrating you how to manually build a Docker image with Node.js and the node-oracledb module. This image can then be used to launch as many application containers as you like. (Depending on your resources off course)
For this setup I have installed Docker on an Oracle Linux 7 VM in VirtualBox.
Create the Docker image manually
Create a Linux base image
Logon as root (or use sudo) on the Oracle Linux VM
First we need an operating system for the container.
Pull docker image of oraclelinux from the Docker hub.
docker pull oraclelinux
Now start an interactive Docker container.
docker run -ti oraclelinux /bin/bash
Within the container we will creating a non-privileged user and install the required OS packages (including dependencies).
The user can be used to run Node.js scripts in the container without root privileges.
useradd nodejs -p '$6$salt$ZjJzVKp5xtoIl7cfXqZe0mQjWeOpsV2pMiIYpWzkR4ExCBpPdT3mi3eXtG1MSawJnZfXFjBcq0UUmenLq1Cj//'
note. I used python to created the encrypted password I used when creating the os user. For your convenience the command:
python -c 'import crypt; print crypt.crypt("Welcome01", "$6$salt$")'
Install the required OS packages including dependencies
yum -y install unzip libaio gcc-c++ tar make curl
Create the base image
Exit the container and commit the container to create a base image.
exit docker ps -a docker commit 51ce97aa511f
Tag the image to give it a name and version, linux-base:1.0
docker images docker tag 19de63788941 linux-base:1.0 docker images
Install Oracle Instantclient, Node.js and the node-oracledb module
Now that we have a base image, we are going to run a new container based on this image.
I have downloaded the Oracle Instantclient from the OTN site and put them in the /tmp/Downloads directory.
Start an interactive container using the created linux-base image and share the /tmp/Downloads directory using a volume in Docker.
docker run -ti -v /tmp/Downloads:/tmp/Downloads linux-base:1.0 /bin/bash
Install Oracle Instantclient
mkdir /opt/oracle cd /opt/oracle unzip -q /tmp/Downloads/instantclient-basic-linux.x64-18.104.22.168.0.zip unzip -q /tmp/Downloads/instantclient-sdk-linux.x64-22.214.171.124.0.zip mv instantclient_12_1 instantclient cd instantclient ln -s libclntsh.so.12.1 libclntsh.so
Use curl to download the Node.js software from nodejs.org and the linux pipe (|) function to pass it to the tar utility which unpacks the software in the /opt/ directory.
cd /opt curl -sSL https://nodejs.org/dist/v4.4.2/node-v4.4.2-linux-x64.tar.xz | tar -xJC /opt/
Install node-oracledb module
The node-oracedb will be installed as global module by the npm (node package manager). Before running npm, set some environment parameters so the node binaries are in the search path and the Oracle libraries can be found.
export PATH=/opt/node-v4.4.2-linux-x64/bin:$PATH export LD_LIBRARY_PATH=/opt/oracle/instantclient:$LD_LIBRARY_PATH npm install -g oracledb
Create the jpoot/node_oracledb image.
exit docker commit a42c4d9b4434 jpoot/node_oracledb:1.0
Testing the created Docker image
Now that I have created the Node.js enabled image, I can test the functionality of it.
I have downloaded examples scripts from de node-oracle/examples on Github.
Start with running a simple select1.js against an Oracle database. This script conects to the database and selects one row from the Departments table.
I have an Oracle Database 126.96.36.199.0 pluggable database running on a separate VM, with the Oracle example schema’s installed in it.
I need to provide some environment variables to provide the PATH, user, password and connect string for the .js scripts to be able to connect to the database.
Create a file called env.list and place the following entries in it.
Explanation of the environment variables
PATH – Add the path to the node and npm binaries to the search path
LD_LIBRARY_PATH – Provides the path to the Oracle libraries
NODE_PATH – Provides the path to the global modules of Node.js
NODE_ORACLE_* – Provides the user, password and connect string to the .js scripts. See dbconfig.js for details
I know, I know… Putting a plaintext password in a file is not secure. Keep in mind that this is for demonstration purposes only.
Don’t do this in any non-demo environment!!!
Run the container with the necessary parameters.
docker run --rm -u nodejs -w /home/nodejs/examples --env-file ./env.list --add-host=db01.domain.local:192.168.100.45 -v /tmp:/home/nodejs/examples jpoot/node_oracledb:1.0 node select1.js
Let’s walk through the parameters of the docker run command I used.
Main docker command to run the container
Remove the container after it has completed
User to run within the container
Working directory, the container starts in this directory
File that contains the environment variables to be provided to the container
Add a host entry for the database server to the /etc/hosts file. This is used because I don’t have a DNS server running.
Add a volume to the container, mapping a local directory to a directory in the running container.
In this case this mounts a directory with the .js scripts I want to run. So you don’t have to add the scripts to the image, making it not dependent on script changes.
Docker image used as base for the container. In this case it is the image I created earlier.
Command to run in the container. Run node command with the select1.js script.
YES!!! The scripts ran correctly in the container.
And now the easy way
In the previous chapters I have created a Docker images that can run Node.js with the capability to connect to an Oracle database.
As you have seen, this involves a lot of manual labor. Manual labor, means greater change of mistakes.
So let’s automate the way to create this image.
In the next steps I will create and use a Dockerfile to automated the creation of the image.
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession.
Create an empty directory to place the Dockerfile in.
I will use the same Instantclient zip files as previously downloaded.
Put these files in the same directory as the Dockerfile for our convenience. (Or create a symlink to the files)
mkdir docker-file cd docker-file vi Dockerfile
# Pull Oracle Linux 7 image from Docker hub
# Install OS packages
RUN yum -y install unzip libaio gcc-c++ tar make curl \
&& useradd nodejs -p ‘$6$salt$ZjJzVKp5xtoIl7cfXqZe0mQjWeOpsV2pMiIYpWzkR4ExCBpPdT3mi3eXtG1MSawJnZfXFjBcq0UUmenLq1Cj//’
# Add Node.js
RUN curl -sSL https://nodejs.org/dist/v4.4.2/node-v4.4.2-linux-x64.tar.xz \
| tar -xJC /opt/
ENV PATH /opt/node-v4.4.2-linux-x64/bin:$PATH
# Add Oracle Instantclient
ADD instantclient-basic-linux.x64-188.8.131.52.0.zip /tmp/
ADD instantclient-sdk-linux.x64-184.108.40.206.0.zip /tmp/
RUN unzip -q /tmp/instantclient-basic-linux.x64-220.127.116.11.0.zip -d /opt/oracle/ \
&& unzip -q /tmp/instantclient-sdk-linux.x64-18.104.22.168.0.zip -d /opt/oracle/ \
&& mv /opt/oracle/instantclient_12_1 /opt/oracle/instantclient \
&& ln -s /opt/oracle/instantclient/libclntsh.so.12.1 /opt/oracle/instantclient/libclntsh.so\
&& rm /tmp/instantclient-*
ENV LD_LIBRARY_PATH /opt/oracle/instantclient
# Install the node-oracledb module as global module to Node.js using npm
RUN npm install -g oracledb
ENV NODE_PATH /opt/node-v4.4.2-linux-x64/lib/node_modules
Build the Docker image
From the docker-file directory run the docker build command an tag the created images with a name and version.
docker build -t="jpoot/node_oracledb:1.1" .
Note. Mind the . at the end of the command. It says, build the image using the Dockerfile here.
Skipping the yum install lines ….
Within two minutes I have a fully functional Docker image ready. J
Now let’s see if the image works.
Remove the PATH, LD_LIBRARY_PATH and NODE_PATH from the env.list.
These environment variables where already provided in the Dockerfile.
docker run --rm -u nodejs -w /home/nodejs/examples --env-file ./env.list --add-host=db01.domain.local:192.168.100.45 -v /tmp:/home/nodejs/examples jpoot/node_oracledb:1.1 node select1.js
Yeah, it works as expected!!!
To test the functionality I used Node.js to run a sql script. The same Docker image can be used to run other .js scripts.
As another example, run a simple webserver.
docker run -d -u nodejs -w /home/nodejs/examples -p 80:3000/tcp -v /tmp:/home/nodejs/examples jpoot/node_oracledb:1.1 node http.js
Btw. Don’t forget to kill this running container when you’re done.
docker kill <container id>
In this article I have shown you two ways to create a Docker image Node.js with a functional node-oracledb module.
There is now right or wrong way to create the image. It is however much easier to use the Dockerfile method. It is fast, easy and prevents human errors. Also, if you want to add modules or functionality to the image just add the commands to the Dockerfile and create a new image in a couple of minutes.
Sources and references
node-oracledb on Github
Oracle Instant Client on OTN
First setup of a connection from Node.js to an Oracle Database
Running node-oracledb – the Oracle Database Driver for Node.js – in the Pre Built VM for Database Development