oracle Docker:试图从托管在docker中的node应用程序连接到另一个服务器中的数据库

1l5u6lss  于 12个月前  发布在  Oracle
关注(0)|答案(1)|浏览(124)

我在docker中托管了一个node应用程序。我需要从这个dockerized应用程序连接到服务器中的oracledb。为了实现这一点,我遵循了教程:Node_Oracledb
我得到的错误是

  • [错误:ORA-12504:TNS:未为侦听器指定SECECT_DATA中的SERVICE_NAME] {错误信息:12504,偏移量:0 }连接未定义 *

现在我不确定这个错误是来自docker还是服务器。下面是代码
我对docker非常陌生,我已经尽我所能从许多其他来源在线获得帮助,但它没有解决这个问题。任何帮助将是伟大的。

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);
})

出口

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

hostname是服务器的别名

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
q3qa4bjr

q3qa4bjr1#

如果你想从一个容器访问另一个容器中运行的Oracle数据库,你应该使用Docker网络。
在运行两个容器之前,创建一个网络:

$ docker network create oraclenet

运行每个容器时,添加--network命令行参数:

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

现在从应用程序连接到Oracle数据库时,只需使用Oracle Server的容器名称作为主机名。假设Oracle数据库容器命名为oracledb

$ export NODE_ORACLEDB_CONNECTIONSTRING=oracledb:1521/servicename

相关问题