我正在尝试使用Oracle数据库连接对一个非常简单的python应用程序进行docker化,并在Docker上执行它。此应用程序在我的本地计算机上执行正常。
我成功地构建了这个应用程序,但在 Docker 上执行它时出现错误。
Docker文件:
FROM python:3
ADD File.py /
RUN pip install cx_Oracle
RUN pip install pandas
RUN pip install openpyxl
CMD [ "python", "./File.py" ]
File.py:
import cx_Oracle
import pandas as pd
#creating database connection
dsn_tns = cx_Oracle.makedsn('dev-tr01.com', '1222', service_name='ast041.com')
conn = cx_Oracle.connect(user=r'usr', password='3451', dsn=dsn_tns)
c = conn.cursor()
query ='SELECT * FROM Employee WHERE ROWNUM <10'
result = pd.read_sql(query, con=conn)
result.to_excel("batchtable.xlsx")
conn.close()
错误:
码头工人运行python_batchdriver:最新
cx_Oracle.数据库错误: DPI-1047: 找不到 64 位 Oracle 客户端库:"libclntsh.so: 无法打开共享对象文件: 没有此类文件或目录"。查看 https://oracle.github.io/odpi/doc/installation.html#linux 以获取帮助
更新:升级到最新的cx_Oracle版本(重命名为 python-oracledb(。 这不一定需要即时客户端,这使得安装变得容易得多。 请参阅发布公告。
对于cx_Oracle,您还需要安装 Oracle 即时客户端库。请参阅cx_Oracle安装说明。
有多种方法可以在 Docker 中自动安装。 一个例子是:
RUN 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 instantclient* &&
rm -f *jdbc* *occi* *mysql* *jar uidrvci genezi adrci &&
echo /opt/oracle/instantclient* > /etc/ld.so.conf.d/oracle-instantclient.conf &&
ldconfig
您还需要 libaio 或 libaio1 软件包。
请参阅 Node.js 和 Python 中的 Docker for Oracle Database Applications 。
另请参阅将 Oracle Instant 客户端安装到 Docker 容器中,用于 Python cx_Oracle 请注意,如果您没有使用基于 Debian 的 Linux 发行版,步骤可能会有所不同。
基于 Debian 的 docker 镜像需要一个 oracle 即时客户端。您可以手动下载它并将其复制到您的 docker 映像。还需要依赖项"libaio1"。
在 docker 文件中添加以下行。
Docker文件:
FROM python:3
# Installing Oracle instant client
WORKDIR /opt/oracle
# Install dependency
RUN apt-get update && apt-get install -y libaio1
# if extracted file is in current directory named "instantclient_11_2"
# For me instantclient_11_2 for Oracle9i, 10g
# copy it to /opt/oracle/instantclient_11_2
COPY instantclient_11_2 /opt/oracle/instantclient_11_2
# Linking instant client and cleanup
RUN 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
# Rest is same as yours
ADD File.py /
# To support instantclient_11_2 I used below commented line
# RUN pip install cx-Oracle==7.3.0
RUN pip install cx_Oracle
RUN pip install pandas
RUN pip install openpyxl
CMD [ "python", "./File.py" ]