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

**URL:** <https://forums.docker.com/t/how-can-a-python-code-in-a-docker-container-access-external-oracle-database/78468>\
**Category:** Docker Hub\
**Created:** [July 22, 2019, 6:32pm UTC](https://forums.docker.com/t/how-can-a-python-code-in-a-docker-container-access-external-oracle-database/78468 "2019-07-22T18:32:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chigala14](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@chigala14](https://forums.docker.com/u/chigala14)\
**Post date:** [July 22, 2019, 6:32pm UTC](https://forums.docker.com/t/how-can-a-python-code-in-a-docker-container-access-external-oracle-database/78468/1 "2019-07-22T18:32:11Z")

</div>

[Please check this link.](https://stackoverflow.com/questions/57084112/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

```auto
docker run -it -net=host image_name

```

But this does not solve the problem.

Here is my docker file-

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![terpz](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/terpz/32/36125_2.png) [@terpz](https://forums.docker.com/u/terpz)\
**Post date:** [July 22, 2019, 7:09pm UTC](https://forums.docker.com/t/how-can-a-python-code-in-a-docker-container-access-external-oracle-database/78468/2 "2019-07-22T19:09:57Z")

</div>

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](https://hub.docker.com/_/python), you need to put this in your dockerfile:

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

```

---

<div class="post-metadata">

**Author:** ![chigala14](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@chigala14](https://forums.docker.com/u/chigala14)\
**Post date:** [July 22, 2019, 7:12pm UTC](https://forums.docker.com/t/how-can-a-python-code-in-a-docker-container-access-external-oracle-database/78468/3 "2019-07-22T19:12:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![terpz](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/terpz/32/36125_2.png) [@terpz](https://forums.docker.com/u/terpz)\
**Post date:** [July 22, 2019, 7:29pm UTC](https://forums.docker.com/t/how-can-a-python-code-in-a-docker-container-access-external-oracle-database/78468/4 "2019-07-22T19:29:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chigala14](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@chigala14](https://forums.docker.com/u/chigala14)\
**Post date:** [August 5, 2019, 4:33pm UTC](https://forums.docker.com/t/how-can-a-python-code-in-a-docker-container-access-external-oracle-database/78468/5 "2019-08-05T16:33:10Z")

</div>

I tried. This does not help either.

---

<div class="post-metadata">

**Author:** ![venki](https://avatars.discourse-cdn.com/v4/letter/v/f0a364/32.png) [@venki](https://forums.docker.com/u/venki)\
**Post date:** [January 30, 2020, 9:37pm UTC](https://forums.docker.com/t/how-can-a-python-code-in-a-docker-container-access-external-oracle-database/78468/6 "2020-01-30T21:37:57Z")

</div>

did you find solution to it?
