docker 打包的python项目运行错误:DPI-1047

docker打包的python项目在linux环境下本地运行能正常查询结果但是通过docker打包部署后出现报错(cx_Oracle.DatabaseError) DPI-1047: Cannot locate a 64-bit Oracle Client library:

1、使用cx_Oracle和 sqlalchemy连接到数据库

项目能正常启动但是不能查询数据

(cx_Oracle.DatabaseError) DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help (Background on this error at: https://sqlalche.me/e/20/4xp6) 2024-01-21 10:07:14,191 - apps.common.logging_config - INFO - (cx_Oracle.DatabaseError) DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help (Background on this error at: https://sqlalche.me/e/20/4xp6)

2、使用cx_oralce升级后的python-oracledb直接打包运行查看docker打印的日志是报错的

[ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/ggevent.py", line 147, in init_process
    super().init_process()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 371, in import_app
    mod = importlib.import_module(module)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed

 解决方案:

在Dockerfile 文件中添加

# Installing Oracle instant client
WORKDIR    /opt/oracle
RUN        apt-get update && apt-get install -y libaio1 wget unzip 
            && wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linuxx64.zip 
            && unzip instantclient-basiclite-linuxx64.zip 
            && rm -f instantclient-basiclite-linuxx64.zip 
            && cd /opt/oracle/instantclient* 
            && rm -f *jdbc* *occi* *mysql* *README *jar uidrvci genezi adrci 
            && echo /opt/oracle/instantclient* > /etc/ld.so.conf.d/oracle-instantclient.conf 
            && ldconfig

 ps:如果使用python-oracledb在项目正常启动后查询报错如下需要添加

oracledb.init_oracle_client()

 (oracledb.exceptions.OperationalError) DPY-6005: cannot connect to database (CONNECTION_ID=eEA4d42d4QA0fN0iyLLnwA==).
DPY-4011: the database or network closed the connection
[Errno 104] Connection reset by peer
Help: https://python-oracledb.readthedocs.io/en/latest/user_guide/troubleshooting.html#dpy-4011
(Background on this error at: https://sqlalche.me/e/20/e3q8)