DEV Community

python1980
python1980

Posted on

How to download output of a pandas data frame from within a Docker container to dedicated folder on host machine?

My docker file

FROM python:latest
VOLUME ["C:/Python"]
WORKDIR /data
COPY . /data
RUN pip install EIA-python
RUN pip install requests
RUN pip install pandas
RUN pip install xlwt
EXPOSE 8888
CMD [ "python", "EIA.py" ]

My Python file

import eia
import pandas as pd
api_key = "MY_API_KEY"
api = eia.API(api_key)
series_storage = api.data_by_series(series='NG.NW2_EPG0_SWO_R48_BCF.W')
df1 = pd.DataFrame(series_storage)
df1.reset_index(inplace=True)
df1.columns = ['Date', 'Value']
df1['Date'] = pd.to_datetime(df1['Date'].str[:-3], format='%Y %m%d')
df1.to_excel("C:/Python/Sid.xls")

What I want my container to do.

When I run my Python file independently it creates an excel file in C:/Python from a pandas data frame. The Python code uses API calls to extract tabular data from public domain.
I would like my Python to do the same when I run a Docker container. How can I achieve this?

When I run my docker command as docker run C:/Python:/data image name I don't see any excel file generated.

Top comments (1)

Collapse
 
cedrickring profile image
Cedric Kring • Edited

As you're executing the Python script in a container now, all paths are different.
df1.to_excel("C:/Python/Sid.xls") is not a valid path in a linux based container. I guess the path should be /data/Sid.xls.

Also make sure to run with docker run -v C:\Python\:/data <image> and allow the containers to access your volumes in Docker for Windows (Settings -> Shared Drives -> Make sure C: is checked).