Write An azure function with Javascript that is connecting OracleDB and running on Docker

  azure-functions, docker, node.js, npm, oracle
  • I have an Azure Function that is written with Visual Studio Code and it is a nodejs application with javascript codes.
  • Also, the application connects to Oracle DB to run an Oracle Script.
  • Also, the application runs on a Docker image.

I added the npm packages for oracle connection;

npm i express
npm i oracledb

Below my some key points of code;

Dockerfile

FROM mcr.microsoft.com/azure-functions/node:3.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot 
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
COPY . /home/site/wwwroot
RUN cd /home/site/wwwroot && 
npm install

index.js

module.exports = async function (context, req) {
let responseMessage = "";
let connection;
try {
    const oracledb = require('oracledb');
    connection = await oracledb.getConnection({
        user: "xx",
        password: "xx",
        connectString: req.body
    });

    let query = 'select * from xx where rownum=1';
    result = await connection.execute(query);
    responseMessage = result;
} catch (err) {
    responseMessage = err.message;
} finally {
    if (connection) {
      try {
        // Always close connections
        await connection.close(); 
      } catch (err) {
        responseMessage = err.message;
      }
    }
  }
context.res = {
    body: responseMessage
};
}

Here is my folder structure of project;

enter image description here

CASE1: When I run the project with "func start" the application is working properly and gets the result.

CASE2: When I run it on my local docker with below steps it returns an error form HTTP response.

  1. Run "docker build ."
  2. docker run -d -p 99:80 myimage
  3. It is listed on "docker ps" list.
  4. I call the endpoint "http://localhost:99/api/HttpExample" I get an error.

DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/node-oracledb/INSTALL.html for help
Node-oracledb installation instructions: https://oracle.github.io/node-oracledb/INSTALL.html
You must have 64-bit Oracle client libraries in LD_LIBRARY_PATH, or configured with ldconfig.
If you do not have Oracle Database on this computer, then install the Instant Client Basic or Basic Light package from
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

I search some documentations but I can’t find the solution for especially Azure Function Project. Because my dockerfile have to be created based on "FROM mcr.microsoft.com/azure-functions/node:3.0".

What should be the Dockerfile for this project?

Source: Docker Questions

LEAVE A COMMENT