Search code examples
pythondockerflaskfilepath

Clarify how to specify file paths inside Docker coming from Python


I have created a Docker image with for a Flask app. Inside my Flask app, this is how I specify the file paths.

dPath = os.getcwd() + "\\data\\distanceMatrixv2.csv"

This should ideally resolve in a filepath similar to app/data/distanceMatrixv2.csv. This works when I do a py main.py run in the CMD. After making sure I am in the same directory, creating the image and running the Docker image via docker run -it -d -p 5000:5000 flaskapp, it throws me the error below when I try to do any processing.

FileNotFoundError: /backend\data\distanceMatrixv2.csv not found.

I believe this is due to how Docker resolves file paths but I am a bit lost on how to "fix" this. I am using Windows while my Docker image is built with FROM node:lts-alpine.


Solution

  • node:lts-alpine is based on Alpine which is a Linux distro. So your python program runs under Linux and should use Linux file paths, i.e. forward slashes like

    dPath = os.getcwd() + "/data/distanceMatrixv2.csv"
    

    It looks like os.getcwd() returns /backend. So the full path becomes /backend/data/distanceMatrixv2.csv.