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: /backenddatadistanceMatrixv2.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
.
2
Answers
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 likeIt looks like
os.getcwd()
returns/backend
. So the full path becomes/backend/data/distanceMatrixv2.csv
.The
os.path
library can do this portably across operating systems. If you write this asit will work whether your (Windows) system uses DOS-style backslash-separated paths or your (Linux) system uses Unix-style forward slashes.
Python 3.4 also added a
pathlib
library, if you prefer a more object-oriented approach. This redefines the Python/
division operator to concatenate paths, using the native path separator. So you could also write