运行保留在 Oracle 数据库中的 Nest.js docker 映像时出错

问题描述 投票:0回答:1

我有一个 Nest.js 应用程序,它在 docker 容器中运行并连接到 Oracle 数据库。

Dockerfile 是:

FROM node:20-alpine

RUN apk add --no-cache libaio unzip

COPY instantclient-basiclite-linux.zip /tmp/

RUN unzip /tmp/instantclient-basiclite-linux.zip -d /usr/lib/oracle && \
    rm /tmp/instantclient-basiclite-linux.zip && \
    ln -s /usr/lib/oracle/instantclient_21_8/libclntsh.so.21.1 /usr/lib/libclntsh.so

WORKDIR /usr/src/api

COPY . .

RUN yarn install

RUN yarn build

EXPOSE 3000

CMD ["yarn", "run", "start:prod"]

数据库配置类为:

import { Injectable, OnModuleDestroy } from '@nestjs/common';
import * as oracledb from 'oracledb';
import { Env } from 'src/env';

@Injectable()
export class DatabaseService implements OnModuleDestroy {
  private connection: oracledb.Connection;

  constructor() {
    this.initializeOracleClient();
  }

  private getClientOptions() {
    if (process.platform === 'win32') {
      return { libDir: 'C:\\oracle\\instantclient_19_22' };
    } else if (process.platform === 'darwin' && process.arch === 'x64') {
      return { libDir: `${process.env.HOME}/Downloads/instantclient_19_8` };
    } else if (process.platform === 'linux') {
      return { libDir: '/usr/lib/oracle/instantclient_21_8' };
    }
    return undefined;
  }
  

  private initializeOracleClient() {
    const clientOpts = this.getClientOptions();
    if (clientOpts) oracledb.initOracleClient(clientOpts);
  }

  private async openConnection() {
    try {
      this.connection = await oracledb.getConnection({
        user: Env.OWNER,
        password: Env.PWD,
        connectString: Env.CONNECT_STRING,
      });
    } catch (error) {
      throw new Error('Falha na conexão com o banco de dados Oracle');
    }
  }

  async executeQuery(query: string, params: any[] = []): Promise<any> {
    if (!this.connection) await this.openConnection();
    try {
      const result = await this.connection.execute(query, params, {
        outFormat: oracledb.OUT_FORMAT_OBJECT,
        autoCommit: true,
      });
      return result.rows;
    } catch (error: any) {
      throw new Error(`Erro ao executar a consulta: ${error.message}`);
    }
  }

  async onModuleDestroy() {
    if (this.connection) {
      try {
        await this.connection.close();
      } catch (error: any) {
        throw new Error(`Erro ao fechar a conexão: ${error.message}`);
      }
    }
  }
  
}

在项目的根目录中存在文件

instantclient-basiclite-linux.zip

构建图像时,我得到:

 *  Executing task: docker build --pull --rm -f "Dockerfile" -t mvallergyagentsearch:latest "." 

[+] Building 6.3s (13/13) FINISHED                                                                                          docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                        0.0s
 => => transferring dockerfile: 540B                                                                                                        0.0s
 => [internal] load metadata for docker.io/library/node:20-alpine                                                                           1.3s
 => [internal] load .dockerignore                                                                                                           0.0s
 => => transferring context: 113B                                                                                                           0.0s
 => [1/8] FROM docker.io/library/node:20-alpine@sha256:c13b26e7e602ef2f1074aef304ce6e9b7dd284c419b35d89fcf3cc8e44a8def9                     0.0s
 => => resolve docker.io/library/node:20-alpine@sha256:c13b26e7e602ef2f1074aef304ce6e9b7dd284c419b35d89fcf3cc8e44a8def9                     0.0s
 => [internal] load build context                                                                                                           0.0s
 => => transferring context: 18.57kB                                                                                                        0.0s
 => CACHED [2/8] RUN apk add --no-cache libaio unzip                                                                                        0.0s
 => CACHED [3/8] COPY instantclient-basiclite-linux.zip /tmp/                                                                               0.0s
 => CACHED [4/8] RUN unzip /tmp/instantclient-basiclite-linux.zip -d /usr/lib/oracle &&     rm /tmp/instantclient-basiclite-linux.zip &&    0.0s
 => CACHED [5/8] WORKDIR /usr/src/api                                                                                                       0.0s
 => CACHED [6/8] COPY . .                                                                                                                   0.0s
 => CACHED [7/8] RUN yarn install                                                                                                           0.0s
 => CACHED [8/8] RUN yarn build                                                                                                             0.0s
 => exporting to image                                                                                                                      4.9s
 => => exporting layers                                                                                                                     0.0s
 => => exporting manifest sha256:ca40a453fb589078768de8d2861fad58c8a53a93d3b88b011b9b967a8b81fee7                                           0.0s
 => => exporting config sha256:8fe58274b8766e67173a018af65077ff4c7be8ead7395cc6ff6ed7d07297a203                                             0.0s
 => => exporting attestation manifest sha256:15f8deb4b3ec5eebf5c5017cdb54fae9e59bd92811742434ed57bb6438293585                               0.0s
 => => exporting manifest list sha256:a59de9a6a7a5a65aab0108a53399dc620302a1aa9cdc764aef7d3fce7497e5ab                                      0.0s
 => => naming to docker.io/library/mvallergyagentsearch:latest                                                                              0.0s
 => => unpacking to docker.io/library/mvallergyagentsearch:latest                                                                           4.8s

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/rcvydlmwn1crtosbq8d4m1oyo

What's next:
    View a summary of image vulnerabilities and recommendations → docker scout quickview 
 *  Terminal will be reused by tasks, press any key to close it.

旋转图像时,我得到:

2024-11-08 13:29:21 yarn run v1.22.22
2024-11-08 13:29:21 $ node dist/main
2024-11-08 13:29:21 [Nest] 27  - 11/08/2024, 4:29:21 PM     LOG [NestFactory] Starting Nest application...
2024-11-08 13:29:21 [Nest] 27  - 11/08/2024, 4:29:21 PM   ERROR [ExceptionHandler] DPI-1047: Cannot locate a 64-bit Oracle Client library: "Error loading shared library /usr/lib/oracle/instantclient_21_8/libclntsh.so: No such file or directory". See https://node-oracledb.readthedocs.io/en/latest/user_guide/installation.html for help
2024-11-08 13:29:21 Node-oracledb installation instructions: https://node-oracledb.readthedocs.io/en/latest/user_guide/installation.html
2024-11-08 13:29:21 You must have Linux arm64 Oracle Client libraries configured with ldconfig, or in LD_LIBRARY_PATH.
2024-11-08 13:29:21 If you do not have Oracle Database on this computer, then install the Instant Client Basic or Basic Light package from 
2024-11-08 13:29:21 https://www.oracle.com/database/technologies/instant-client.html
2024-11-08 13:29:21 
2024-11-08 13:29:21 Error: DPI-1047: Cannot locate a 64-bit Oracle Client library: "Error loading shared library /usr/lib/oracle/instantclient_21_8/libclntsh.so: No such file or directory". See https://node-oracledb.readthedocs.io/en/latest/user_guide/installation.html for help
2024-11-08 13:29:21 Node-oracledb installation instructions: https://node-oracledb.readthedocs.io/en/latest/user_guide/installation.html
2024-11-08 13:29:21 You must have Linux arm64 Oracle Client libraries configured with ldconfig, or in LD_LIBRARY_PATH.
2024-11-08 13:29:21 If you do not have Oracle Database on this computer, then install the Instant Client Basic or Basic Light package from 
2024-11-08 13:29:21 https://www.oracle.com/database/technologies/instant-client.html
2024-11-08 13:29:21 
2024-11-08 13:29:21     at Object.initOracleClient (/usr/src/api/node_modules/oracledb/lib/oracledb.js:710:20)
2024-11-08 13:29:21     at DatabaseService.initializeOracleClient (/usr/src/api/dist/service/database.service.js:35:22)
2024-11-08 13:29:21     at new DatabaseService (/usr/src/api/dist/service/database.service.js:18:14)
2024-11-08 13:29:21     at Injector.instantiateClass (/usr/src/api/node_modules/@nestjs/core/injector/injector.js:365:19)
2024-11-08 13:29:21     at callback (/usr/src/api/node_modules/@nestjs/core/injector/injector.js:65:45)
2024-11-08 13:29:21     at async Injector.resolveConstructorParams (/usr/src/api/node_modules/@nestjs/core/injector/injector.js:144:24)
2024-11-08 13:29:21     at async Injector.loadInstance (/usr/src/api/node_modules/@nestjs/core/injector/injector.js:70:13)
2024-11-08 13:29:21     at async Injector.loadProvider (/usr/src/api/node_modules/@nestjs/core/injector/injector.js:97:9)
2024-11-08 13:29:21     at async /usr/src/api/node_modules/@nestjs/core/injector/instance-loader.js:56:13
2024-11-08 13:29:21     at async Promise.all (index 6)
2024-11-08 13:29:21 error Command failed with exit code 1.
2024-11-08 13:29:21 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

如何解决这个问题?

node.js oracle-database docker nestjs
1个回答
0
投票

错误信息是:

加载共享库 /usr/lib/oracle/instantclient_21_8/libclntsh.so 时出错:没有这样的文件或目录

您的跑步正在做:

    ln -s /usr/lib/oracle/instantclient_21_8/libclntsh.so.21.1 /usr/lib/libclntsh.so

您应该将其链接到同一目录中:

    ln -s /usr/lib/oracle/instantclient_21_8/libclntsh.so.21.1 /usr/lib/oracle/instantclient_21_8/libclntsh.so
© www.soinside.com 2019 - 2024. All rights reserved.