【问题标题】:Install Oracle Instant client into Docker container for Python cx_Oracle将 Oracle Instant 客户端安装到 Python cx_Oracle 的 Docker 容器中
【发布时间】:2020-03-18 10:47:53
【问题描述】:

我正在尝试通过我的 docker 容器连接到我公司的 Oracle 数据库,该容器包含我的一些带有包 cx_Oracle 的 python 脚本。构建并运行容器后,出现以下错误:

conn = cx_Oracle.connect("{0}/{1}@{2}".format(configOracle["username"], configOracle["password"],r"ed03:1521/configOracle["servername"]))
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://oracle.github.io/odpi/doc/installation.html#linux for help

我有一个 Oracle 配置文件,其中用户名、密码和服务器名称来自并正确填写。即使从https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html 下载了最新的客户端,我似乎也无法让它工作。

我的目录结构如下:

--TopDirectory
----instantclient
-------instantclient-basic-linux.x64-19.5.0.0.0dbru.zip
-------instantclient-sdk-linux.x64-19.5.0.0.0dbru.zip
----hello_oracle.py
----Dockerfile
----requirements.txt
----configOracle.json

这是我的 Dockerfile:

FROM python:3.7.5

#Oracle Client setup
ENV ORACLE_HOME /opt/oracle/instantclient_19_5
ENV LD_RUN_PATH=$ORACLE_HOME

COPY instantclient/* /tmp/

RUN \
    mkdir -p /opt/oracle && \
    unzip "/tmp/instantclient*.zip" -d /opt/oracle && \
    ln -s $ORACLE_HOME/libclntsh.so.19.1 $ORACLE_HOME/libclntsh.so

# Working directory
WORKDIR /src
# Copying requirements.txt before entire build step
COPY requirements.txt /src/requirements.txt

RUN pip install --upgrade pip
# Installing necessary packages
RUN pip install -r requirements.txt
# Copying rest of files
COPY . /src
CMD ["python3", "/src/hello_oracle.py"]

这是我的 requirements.txt 文件:

pandas
numpy
matplotlib
keras
cx_Oracle
sklearn
tensorflow
pyopenssl
ndg-httpsclient
pyasn1

【问题讨论】:

    标签: python oracle docker dockerfile containers


    【解决方案1】:

    Oracle 在https://github.com/oracle/docker-images/tree/master/OracleLinuxDevelopers 有 Python Dockerfiles

    更新:Oracle 在https://github.com/orgs/oracle/packages 有容器

    有一个由两部分组成的博文系列Docker for Oracle Database Applications in Node.js and Python,展示了各种安装方式。还有一个讨论 cx_Oracle 和 Docker here 的 Oracle 网络广播录音。

    例如,一种解决方案是使用:

    FROM python:3.7.4-slim-buster
    
    RUN apt-get update && apt-get install -y libaio1 wget unzip
    
    WORKDIR /opt/oracle
    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 /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
    RUN python -m pip install cx_Oracle
    

    如果您使用不同的基础映像,您可能不需要显式安装 unzip 或 libaio1。如果您对使用 ADD 感到满意,那么您也不需要 wget。

    【讨论】:

      【解决方案2】:

      经过几个小时的尝试,我终于用这个 Dockerfile 解决了这个问题

      注意 我正在使用 python 3.7、Django 3.0、Oracle Database 12c 和 Pipenv 进行包管理

      FROM python:3.7.5-slim-buster
      
      # 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
      
      WORKDIR    /app
      COPY       . .  # Copy my project folder content into /app container directory
      RUN        pip3 install pipenv
      RUN        pipenv install
      EXPOSE     8000
      # For this statement to work you need to add the next two lines into Pipfilefile
      # [scripts]
      # server = "python manage.py runserver 0.0.0.0:8000"
      ENTRYPOINT ["pipenv", "run", "server"]
      

      【讨论】:

      • 很好的例子,我发现你的代码中的python3 manage.py runserver 0.0.0.0:8000 非常友好。谢谢!
      • 您已经描述了如何为 linux 获取即时客户端。你能推荐一下windows的地址吗?没看懂
      猜你喜欢
      • 2019-09-15
      • 1970-01-01
      • 2015-01-18
      • 2013-03-22
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 2011-03-21
      • 2019-06-24
      相关资源
      最近更新 更多