Docker: Trying to connect to a database which is in another server from node app hosted in docker

175 Views Asked by At

I have a node app hosted in docker. I need to connect to a oracledb which is in a server from this dockerized app. To fulfil this I followed the tutorial: Node_Oracledb

Error I get is

[Error: ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA] { errorNum: 12504, offset: 0 } connection is not defined

Now I am not sure this error is coming from docker or server. Following is the code

I am very new to docker and I have tried my best to get help online from many other sources but it didnt solve the issue. Any help would be great.

dbConfig.js

module.exports = {
user          : process.env.NODE_ORACLEDB_USER,
password      : process.env.NODE_ORACLEDB_PASSWORD,
connectString : process.env.NODE_ORACLEDB_CONNECTIONSTRING,
poolMax: 2,
poolMin: 2,
poolIncrement: 0

};

Dockerfile

# bring latest node in alpine linux image
FROM oraclelinux:7-slim
# app directory
WORKDIR /app
#copy pacakge*.json files
COPY package*.json ./

# Update Oracle Linux
# Install Node.js
# Install the Oracle Instant Client
# Check that Node.js and NPM installed correctly
# Install the OracleDB driver
RUN yum update -y && \
yum install -y oracle-release-el7 && \
yum install -y oracle-nodejs-release-el7 && \
yum install -y nodejs && \
yum install -y oracle-instantclient19.3-basic.x86_64 && \
yum clean all && \
node --version && \
npm --version && \
npm install && \
echo Installed

# Copy project
COPY . .

EXPOSE 9090

CMD ["node", "index.js"]

index.js

const express = require('express');
const oracledb = require('oracledb');
const dbConfig = require('./dbConfig.js');
const app = express();

oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT;
oracledb.autoCommit = true;

async function getData(req, res) {
    try {
        connection = await oracledb.getConnection(dbConfig);
        data = await connection.execute(`SELECT * FROM DEPARTMENT`)
    } catch (error) {
        return res.send(error.message)
    } finally {
        try {
            await connection.close();
            console.log('Connection closed');
        } catch (error) {
            console.error(error.message)
        }
    }
    if (data.rows.length == 0) {
        return res.send('No data found');
    } else {
        return res.status(200).json({
            data: data.rows, 
        })
    }
}

app.get('/', function(req, res) {
    getData(req, res);
})

exports

export NODE_ORACLEDB_USER=NotMyRealUser
export NODE_ORACLEDB_PASSWORD=NotMyRealPassword
export NODE_ORACLEDB_CONNECTIONSTRING=hostname:1521/servicename

hostname is the alias of the server

docker build

docker build --no-cache --force-rm=true -t node_oracle_app .

docker run

docker run -it -p 9090:9090 -e NODE_ORACLEDB_USER=$NODE_ORACLEDB_USER -e NODE_ORACLEDB_PASSWORD=$NODE_ORACLEDB_PASSWORD -e NODE_ORACLEDB_CONNECTIONSTRING=$NODE_ORACLEDB_CONNECTIONSTRING ade_openlot3_server_oracle
1

There are 1 best solutions below

4
doberkofler On

I you want to access an Oracle Database running in one container from another container you should use a docker network.

Before running the two container create a network:

$ docker network create oraclenet

When running each container add the --network command line argument:

$ docker run ... --network="oraclenet"

When now connecting to the Oracle Database from you application, just use the container name of the Oracle Server as the host name. Assuming the Oracle Database container is named oracledb:

$ export NODE_ORACLEDB_CONNECTIONSTRING=oracledb:1521/servicename