How to access CSV file (located in pc hdd) from a docker container with python pandas?

穿精又带淫゛_ 提交于 2019-12-19 11:43:04

问题


I want to implement a Machine Learning algorithm which can operate on homomorphic data using PySEAL library. PySEAL library is released as a docker container with an 'examples.py' file which shows some homomorphic encryption example. I want to edit the 'examples.py' file to implement the ML algorithm. I trying to import a CSV file in this way -

dataset = pd.read_csv ('Dataset.csv')

I have imported pandas library successfully. I have tried many approaches to import the CSV file but failed. How can I import it?

I am new to Docker. Detailed procedure will be really helpful.


回答1:


You can either do it via the Docker build process (assuming you are the one creating the image) or through a volume mapping that would be accessed by the container during runtime.

Building source with Dataset.csv within

For access through the build, you could do a Docker Copy command to get the file within the workspace of the container

FROM 3.7

COPY /Dataset.csv /app/Dataset.csv
...

Then you can directly access the file via /app/Dataset.csv from the container using pandas.read_csv() function, like -

data=pandas.read_csv('/app/Dataset.csv')

Mapping volume share for Dataset.csv

If you don't have direct control over the source image creation, or do not want the dataset packaged with the container (which may be the best practice depending on the use case). You can share it through a volume mapping when starting the container:

dataset = pd.read_csv ('app/Dataset.csv')

Assuming your Dataset.csv is in my/user/dir/Dataset.csv

From CLI:

docker run -v my/user/dir:app my-python-container

The benefit of the latter solution is you can then continue to edit the file 'Dataset.csv' on your host and the file will reflect changes made by you OR the python process should that occur.



来源:https://stackoverflow.com/questions/56992186/how-to-access-csv-file-located-in-pc-hdd-from-a-docker-container-with-python-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!