简体   繁体   English

docker容器中的python代码如何访问外部oracle数据库?

[英]How can a python code in a docker container access external oracle database?

I am able to connect/ ping to my external database on my Windows host machine. 我能够在Windows主机上连接/ ping我的外部数据库。 Also, I am able to ping the same network from my docker Quickstart terminal. 另外,我能够从docker Quickstart终端ping通相同的网络。

The external database is on another server probably behind the company's firewall. 外部数据库位于公司防火墙后面的另一台服务器上。

However, when I try to run the container, which has a python file that connects to an Oracle database, I get this error: 但是,当我尝试运行具有连接到Oracle数据库的python文件的容器时,出现此错误:

ORA-12170: TNS:Connect timeout occurred ORA-12170:TNS:发生连接超时

However, I can run the python file independently without the containers. 但是,我可以在没有容器的情况下独立运行python文件。

It seems like, the container is configured on another network and might not have access to the oracle database. 看来,该容器是在另一个网络上配置的,可能无法访问oracle数据库。

I have tried using 我尝试使用

docker run -it -net=host image_name

But this does not solve the problem. 但这不能解决问题。

Here is my docker file- 这是我的docker文件-

# INSTALL PYTHON IMAGE
FROM python:3.7.2-slim
RUN apt-get update \
    && apt-get -y install unzip \
    && apt-get -y install libaio-dev \
    && apt-get install -y iputils-ping \
    && apt-get -y install sudo \
    && mkdir -p /opt/data/app

ADD ./oracle-instantclient/ /opt/data
ADD ./requirements.txt /opt/data
ADD ./app/ /opt/data/app
WORKDIR /opt/data
ENV ORACLE_HOME=/opt/data/oracle-instantclient/instantclient-basic-linux.x64-12.1.0.2.0/instantclient_12_1
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
ENV OCI_HOME=/opt/data/oracle-instantclient/instantclient-basic-linux.x64-12.1.0.2.0/instantclient_12_1
ENV OCI_LIB_DIR=/opt/data/oracle-instantclient/instantclient-basic-linux.x64-12.1.0.2.0/instantclient_12_1
ENV OCI_INCLUDE_DIR=/opt/data/oracle-instantclient/instantclient-basic-linux.x64-12.1.0.2.0/instantclient_12_1

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python","./app/Oracle_ETL.py"]

Here is an extract from my python file: 这是我的python文件的摘录:

import cx_Oracle
import pandas as pd
db = cx_Oracle.connect('Username/Password@host:port/db_name')
select_sql = 'Select * from temp_table'
df_temp = pd.read_sql(select_sql, con=db)
.
.
.

I would like to know how do we run this python file from inside the container. 我想知道我们如何从容器内部运行此python文件。

According to Docker documentation, option to use the host network is --network="host" . 根据Docker文档,使用主机网络的选项是--network =“ host” See if that can resolve your problem. 看看是否可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM