Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - Writing python output to a csv file in the current working directory

I want to learn how to deploy applications using Docker and I'm working with this simple python program that writes some data into a csv file in the current working directory. I can see the output.csv file in the current working directory on my local machine but having trouble with it when I run the docker image.

After reading various articles and posts on stackoverflow, mounting the local directory path using "-v" seems to be the way to achieve this but I am unable get the right command. I tried creating a new directory on my local machine named "output_docker" ( I also replaced the get.cwd() option in the program with this path) and use that with the -v option.

 docker container run -v "/Users/Desktop/output_docker" docker_image_name

Python code (scraper.py)

import pandas as pd
import os

data = [['tom', 10], ['nick', 15], ['juli', 14]]
df = pd.DataFrame(data, columns = ['Name', 'Age'])

dirpath = os.getcwd()
print("dirpath = ", dirpath, "\n")

output_path = os.path.join(dirpath,'output.csv')
print(output_path,"\n")

df.to_csv(output_path)

DOCKERFILE

FROM python:3
ADD scraper.py /
RUN pip install pandas
CMD ["python3","./scraper.py"]

Output on local machine

$python3 scraper.py
   Name  Age
0   tom   10
1  nick   15
2  juli   14

dirpath =  /Users/Prathusha/Desktop/topos_docker 

/Users/Prathusha/Desktop/topos_docker/output.csv 

Output when I run the docker image

$docker build -t ex_scraper .
$docker run ex_scraper
   Name  Age
0   tom   10
1  nick   15
2  juli   14
dirpath =  / 

/output.csv 

I understand that the "output.csv" file will be located within the subdirectories of the docker container, but I want it to be visible in the current working directory(or desktop) when the docker image is run on a different machine. Would appreciate if anyone can point out where I am going wrong.

like image 240
PN07815 Avatar asked May 24 '19 06:05

PN07815


1 Answers

You can bind your host directory, I would suggest using a WORKDIR & replace ADD with COPY -

DOCKERFILE

FROM python:3
WORKDIR /mydata
COPY scraper.py ./
RUN pip install pandas
CMD ["python3","./scraper.py"]

Run it -

docker run -v ${PWD}:/data ex_scraper

You should now be able to see the CSV in your current directory on host.

like image 91
vivekyad4v Avatar answered Nov 15 '22 06:11

vivekyad4v