Praveenks
Praveenks

Reputation: 1494

How to access generated output file in Docker

I have dockerized my python application. This application connects with Oracle database, pull out 10 rows from a table and then generate excel. I was able to build my image successfully with all dependent libraries and it's also executing fine. Now, I am not sure how to get generated excel file (batchtable.xlsx) in docker. I am new to docker and would need your suggestion. I have checked output without storing records into excel and it's coming fine on console, so there is no code issue.

Dockerfile

FROM python:3.7.4-slim-buster

RUN apt-get update && apt-get install -y libaio1 wget unzip

WORKDIR /opt/oracle
COPY File.py /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 --upgrade pip 
RUN python -m pip install cx_Oracle
RUN python -m pip install pandas
RUN python -m pip install openpyxl

CMD [ "python", "/opt/oracle/File.py" ]

File.py

import cx_Oracle
import pandas as pd

#creating database connection
dsn_tns = cx_Oracle.makedsn('dev-tr01.com', '1222', service_name='ast041.com')
conn = cx_Oracle.connect(user=r'usr', password='3451', dsn=dsn_tns)
c = conn.cursor()

query ='SELECT * FROM Employee WHERE ROWNUM <10'
result = pd.read_sql(query, con=conn)
result.to_excel("batchtable.xlsx")

conn.close()

Upvotes: 2

Views: 2840

Answers (2)

Arnaud Weil
Arnaud Weil

Reputation: 2502

  1. Add a -v switch to your docker run command. For instance:

    docker run -v <path>:/output YOUR_IMAGE_NAME
    

Replace path with a valid path on your machine, for instance c:\temp on Windows.

  1. Change your program to write to that directory:

    result.to_excel("/output/batchtable.xlsx")
    
  2. If running Docker Desktop, in the Docker Desktop settings make sure your drive is shared.

Upvotes: 1

invad0r
invad0r

Reputation: 926

You can access your data by mounting a volume into your container e.g.

docker run -ti -v $(pwd):/data IMAGE

https://docs.docker.com/storage/volumes/#start-a-container-with-a-volume

Upvotes: 3

Related Questions