Flask App file location errror

Hi all,

I’m having an issue with my Docker container whereby it can’t seem to find csv file that is called by my main.py Of course, the typical conundrum being that this works perfectly on my own local machine. The csv file is in a static folder which is held at the same level as my main.py file. In my main.py file the code that reads the file is

@app.route('/')
def index():
    #Read data into a pandas dataframe
    data = pd.read_csv("./static/file.csv")
    print(data.to_json(orient="records"))
    return render_template('index.html',data = data.to_json(orient="records"))

On my Docker container the log shows the error as

FileNotFoundError: [Errno 2] No such file or directory: ‘./static/file.csv’

My Dockerfile looks like this:

FROM python:3.12

# set the working directory

WORKDIR /flaskapp

# install dependencies

COPY ./requirements.txt /flaskapp

RUN pip install --no-cache-dir --upgrade -r requirements.txt

# Copy your Flask application code

COPY . /flaskapp

# Expose the port where the Flask app will run (usually 5000)

EXPOSE 5000

# Run the Flask app using gunicorn (recommended for production)

CMD ["gunicorn", "--bind", "0.0.0.0:5000", "main:app"]

Has anyone had this issue before or know what the issue may be?
Any help would be greatly appreciated. Thanks!

Since you have the sourcecode, have you tried changing the code to list the directory of the file to see what gunicorn can see? If the workdir is right, then maybe copying the files didn’t work as you expected. I had some other debugging tips in a video which can be found here too: