Docker 上的 cx_Oracle:DPI-1047:无法加载 64 位 Oracle 客户端库:“libclntsh.so

Posted

技术标签:

【中文标题】Docker 上的 cx_Oracle:DPI-1047:无法加载 64 位 Oracle 客户端库:“libclntsh.so【英文标题】:cx_Oracle on Docker : DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so 【发布时间】:2020-06-19 21:49:52 【问题描述】:

我正在尝试使用 cx_Oracle 构建 Docker 映像。

这是我正在使用的 Dockerfile:

FROM oraclelinux:7-slim

ARG release=18
ARG update=3

RUN  yum -y install oracle-release-el7 && \
     yum-config-manager --enable ol7_oracle_instantclient && \
     yum -y install oracle-instantclient$release.$update-basic && \
     rm -rf /var/cache/yum

COPY ./fetch_session_iptv.py /opt/
COPY ./conf/fetch_session_iptv.conf /opt/conf/
#COPY ./conf/certs/* /opt/conf/certs/
COPY ./logs /opt/logs

RUN yum install -y \
    #https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/oracle-instantclient18.3-basic-18.3.0.0.0-2.x86_64.rpm \
    https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/python36-cx_Oracle-7.0-1.0.2.el7.x86_64.rpm \
    https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/python36-pytz-2016.10-2.0.1.el7.noarch.rpm

COPY ./python_requirements/elasticsearch-7.8.0-py2.py3-none-any.whl .
COPY ./python_requirements/certifi-2020.4.5.2-py2.py3-none-any.whl .
COPY ./python_requirements/urllib3-1.25.9-py2.py3-none-any.whl .

RUN pip3 install --user \
    certifi-2020.4.5.2-py2.py3-none-any.whl \
    urllib3-1.25.9-py2.py3-none-any.whl \
    elasticsearch-7.8.0-py2.py3-none-any.whl


#CMD ["/bin/bash"]

CMD [ "python3", "/opt/fetch_session_iptv.py" ]

这直接来自 Oracle 文档:ODPI-C Installation

图像似乎正确构建:

Sending build context to Docker daemon  3.214GB
Step 1/13 : FROM oraclelinux:7-slim
 ---> 7442ca158991
Step 2/13 : ARG release=18
 ---> Using cache
 ---> a9601d8b5721
Step 3/13 : ARG update=3
 ---> Using cache
 ---> 9c82b94480af
Step 4/13 : RUN  yum -y install oracle-release-el7 &&      yum-config-manager --enable ol7_oracle_instantclient &&      yum -y install oracle-instantclient$release.$update-basic &&      rm -rf /var/cache/yum
 ---> Using cache
 ---> aadfe1be7fe3
Step 5/13 : COPY ./fetch_session_iptv.py /opt/
 ---> Using cache
 ---> b3346172a4d4
Step 6/13 : COPY ./conf/fetch_session_iptv.conf /opt/conf/
 ---> Using cache
 ---> a0b0def735fc
Step 7/13 : COPY ./logs /opt/logs
 ---> Using cache
 ---> 32e1a82972f7
Step 8/13 : RUN yum install -y  https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/python36-cx_Oracle-7.0-1.0.2.el7.x86_64.rpm     https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/python36-pytz-2016.10-2.0.1.el7.noarch.rpm
 ---> Running in 50a6545982aa
Loaded plugins: ovl
Examining /var/tmp/yum-root-5QFxy8/python36-cx_Oracle-7.0-1.0.2.el7.x86_64.rpm: python36-cx_Oracle-7.0-1.0.2.el7.x86_64
Marking /var/tmp/yum-root-5QFxy8/python36-cx_Oracle-7.0-1.0.2.el7.x86_64.rpm to be installed
Examining /var/tmp/yum-root-5QFxy8/python36-pytz-2016.10-2.0.1.el7.noarch.rpm: python36-pytz-2016.10-2.0.1.el7.noarch
Marking /var/tmp/yum-root-5QFxy8/python36-pytz-2016.10-2.0.1.el7.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package python36-cx_Oracle.x86_64 0:7.0-1.0.2.el7 will be installed
--> Processing Dependency: python(abi) = 3.6 for package: python36-cx_Oracle-7.0-1.0.2.el7.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: python36-cx_Oracle-7.0-1.0.2.el7.x86_64
---> Package python36-pytz.noarch 0:2016.10-2.0.1.el7 will be installed
--> Running transaction check
---> Package python3.x86_64 0:3.6.8-13.0.1.el7 will be installed
--> Processing Dependency: python3-pip for package: python3-3.6.8-13.0.1.el7.x86_64
--> Processing Dependency: python3-setuptools for package: python3-3.6.8-13.0.1.el7.x86_64
---> Package python3-libs.x86_64 0:3.6.8-13.0.1.el7 will be installed
--> Processing Dependency: libtirpc.so.1()(64bit) for package: python3-libs-3.6.8-13.0.1.el7.x86_64
--> Running transaction check
---> Package libtirpc.x86_64 0:0.2.4-0.16.el7 will be installed
---> Package python3-pip.noarch 0:9.0.3-7.el7_8 will be installed
---> Package python3-setuptools.noarch 0:39.2.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch   Version           Repository                    Size
================================================================================
Installing:
 python36-cx_Oracle x86_64 7.0-1.0.2.el7     /python36-cx_Oracle-7.0-1.0.2.el7.x86_64
                                                                          1.3 M
 python36-pytz      noarch 2016.10-2.0.1.el7 /python36-pytz-2016.10-2.0.1.el7.noarch
                                                                          919 k
Installing for dependencies:
 libtirpc           x86_64 0.2.4-0.16.el7    ol7_latest                    89 k
 python3            x86_64 3.6.8-13.0.1.el7  ol7_latest                    69 k
 python3-libs       x86_64 3.6.8-13.0.1.el7  ol7_latest                   7.0 M
 python3-pip        noarch 9.0.3-7.el7_8     ol7_latest                   1.8 M
 python3-setuptools noarch 39.2.0-10.el7     ol7_latest                   628 k

Transaction Summary
================================================================================
Install  2 Packages (+5 Dependent packages)

Total size: 12 M
Total download size: 9.5 M
Installed size: 50 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              1.5 MB/s | 9.5 MB  00:06     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libtirpc-0.2.4-0.16.el7.x86_64                               1/7 
  Installing : python3-libs-3.6.8-13.0.1.el7.x86_64                         2/7 
  Installing : python3-setuptools-39.2.0-10.el7.noarch                      3/7 
  Installing : python3-pip-9.0.3-7.el7_8.noarch                             4/7 
  Installing : python3-3.6.8-13.0.1.el7.x86_64                              5/7 
  Installing : python36-cx_Oracle-7.0-1.0.2.el7.x86_64                      6/7 
  Installing : python36-pytz-2016.10-2.0.1.el7.noarch                       7/7 
  Verifying  : python3-3.6.8-13.0.1.el7.x86_64                              1/7 
  Verifying  : libtirpc-0.2.4-0.16.el7.x86_64                               2/7 
  Verifying  : python36-cx_Oracle-7.0-1.0.2.el7.x86_64                      3/7 
  Verifying  : python3-libs-3.6.8-13.0.1.el7.x86_64                         4/7 
  Verifying  : python3-setuptools-39.2.0-10.el7.noarch                      5/7 
  Verifying  : python36-pytz-2016.10-2.0.1.el7.noarch                       6/7 
  Verifying  : python3-pip-9.0.3-7.el7_8.noarch                             7/7 

Installed:
  python36-cx_Oracle.x86_64 0:7.0-1.0.2.el7                                     
  python36-pytz.noarch 0:2016.10-2.0.1.el7                                      

Dependency Installed:
  libtirpc.x86_64 0:0.2.4-0.16.el7           python3.x86_64 0:3.6.8-13.0.1.el7  
  python3-libs.x86_64 0:3.6.8-13.0.1.el7     python3-pip.noarch 0:9.0.3-7.el7_8 
  python3-setuptools.noarch 0:39.2.0-10.el7 

Complete!
Removing intermediate container 50a6545982aa
 ---> 3345ba39b755
Step 9/13 : COPY ./python_requirements/elasticsearch-7.8.0-py2.py3-none-any.whl .
 ---> dcc4f0c5a5c5
Step 10/13 : COPY ./python_requirements/certifi-2020.4.5.2-py2.py3-none-any.whl .
 ---> e47c23f75225
Step 11/13 : COPY ./python_requirements/urllib3-1.25.9-py2.py3-none-any.whl .
 ---> f850ea09150d
Step 12/13 : RUN pip3 install --user    certifi-2020.4.5.2-py2.py3-none-any.whl urllib3-1.25.9-py2.py3-none-any.whl     elasticsearch-7.8.0-py2.py3-none-any.whl
 ---> Running in d01bf60be689
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Processing /certifi-2020.4.5.2-py2.py3-none-any.whl
Processing /urllib3-1.25.9-py2.py3-none-any.whl
Processing /elasticsearch-7.8.0-py2.py3-none-any.whl
Installing collected packages: certifi, urllib3, elasticsearch
Successfully installed certifi-2020.4.5.2 elasticsearch-7.8.0 urllib3-1.25.9
Removing intermediate container d01bf60be689
 ---> 4d38468f3a00
Step 13/13 : CMD [ "python3", "/opt/fetch_session_iptv.py" ]
 ---> Running in c2fdb466cd69
Removing intermediate container c2fdb466cd69
 ---> a614ef5071ac
Successfully built a614ef5071ac
Successfully tagged kansas-fetch:1.5-SNAPSHOT

但是当我尝试运行它时,我收到以下错误消息,即使应该安装 Oracle 客户端:

Traceback (most recent call last):
python_metro_kansas_sql |   File "/opt/fetch_session_iptv.py", line 27, in <module>
python_metro_kansas_sql |     with cx_Oracle.connect("test","test", "test") as connection:
python_metro_kansas_sql | cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help

知道如何解决这个问题吗?

我尝试使用不同版本的 Oracle Client 和 cx_Oracle,但没有区别。

【问题讨论】:

【参考方案1】:

对于 19c 之前的 RPM,您需要在安装 Instant Client RPM 时更新系统库搜索路径。推荐的方式是使用ldconfig,如Oracle's sample 18c Dockerfile所示:

RUN  yum -y install oracle-release-el7 \
     yum -y install oracle-instantclient$release.$update-basic oracle-instantclient$release.$update-devel oracle-instantclient$release.$update-sqlplus && \
     rm -rf /var/cache/yum && \
     echo /usr/lib/oracle/$release.$update/client64/lib > /etc/ld.so.conf.d/oracle-instantclient$release.$update.conf && \
     ldconfig

但是,您应该改用 19c,它会在 RPM 包安装期间自动执行此操作,请参阅 sample Instant Client 19c Dockerfile。 19c 版本将让您的应用程序连接到与 18c Instant Client 相同的数据库版本,因此使用旧版本没有任何意义。

(注意使用 Instant Client 19c ZIP 文件仍需要手动设置系统库搜索路径)。

【讨论】:

github.com/oracle/docker-images/tree/master/…上的 Oracle Linux 组有一些 Python Dockerfiles

以上是关于Docker 上的 cx_Oracle:DPI-1047:无法加载 64 位 Oracle 客户端库:“libclntsh.so的主要内容,如果未能解决你的问题,请参考以下文章

Python安装cx_oracle操作

第八篇:cx_Oracle出现的问题

oracle 错误代码处理 cx_oracle

cx_Oracle.DatabaseError: ORA-12514

使用 cx_oracle 批量下载表

Python源码安装cx_Oracle