简体   繁体   中英

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. Also, I am able to ping the same network from my docker Quickstart terminal.

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:

ORA-12170: TNS:Connect timeout occurred

However, I can run the python file independently without the containers.

It seems like, the container is configured on another network and might not have access to the oracle database.

I have tried using

docker run -it -net=host image_name

But this does not solve the problem.

Here is my docker file-

# 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:

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.

According to Docker documentation, option to use the host network is --network="host" . See if that can resolve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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