Docker Community Forums

Share and learn in the Docker community.

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

Please check this link.

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.

Hi

Just to be sure, its double-dash: --net=host

And how to run, according to the base image you’re using, https://hub.docker.com/_/python, you need to put this in your dockerfile:

CMD [ "python", "./your-daemon-or-script.py" ]

Hi Martin,
Thanks for the prompt response.

I tried with double dashes but it did not work.

Also, my docker file has the command you suggested.

Just for fun, can you try and instead of host, use the IP of the remote server?

I tried. This does not help either.