Skip to content
Advertisement

how to read a file from a local directory from python code in a docker container?

I have tried to containerize my python code , this is working, building the docker image, running the container and invoking the python code in my app.py file. next, i want to be able to upload the sample.txt file that my python code reads from the root of the application to process some data, sample.txt in the example below. but this file needs to be updated and i want to be able to update this file and run the container, without building the image. how can i create/mount a directory and put the csv file there and give the path to my python code?

app.py

JavaScript

docker file

JavaScript

requirements.txt

JavaScript

Advertisement

Answer

When you docker run, you can (bind) mount folders/files on the host as folders/files on the container:

JavaScript

For example if you had example.csv on the host’s root directory and you want it to be accessible to your Python app in the container in /data, you would:

JavaScript

NOTE That the file name must be included in the host-path and container-path elements of --volume=host-path:container-path permitting you to rename the host’s file (example.csv) to something else on the container too.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement