I am trying to run a simple Flask application inside a Docker container on a WSL (Windows Subsystem for Linux) environment. The Flask app is supposed to list files in a specified folder, which is provided via a JSON payload in a POST request. When I run the Flask app directly in WSL, it works perfectly. However, when I run it inside a Docker container, it fails to access the specified folder path.
The application should allow any user to specify any folder path on their local machine, and the script should be able to open those files and list them.
Project Structure
/home/asay/docker_test/
├── app.py
├── Dockerfile
├── docker-compose.yml
└── requirements.txt
app.py
from flask import Flask, request, jsonify
import os
app = Flask(__name__)
@app.route('/list_files', methods=['POST'])
def list_files():
data = request.get_json()
folder_path = data.get('folder_path')
if not folder_path:
return jsonify({"error": "Folder path is required"}), 400
# Convert Windows path to WSL path
folder_path = folder_path.replace('C:', '/mnt/c').replace('\', '/')
if not os.path.exists(folder_path):
return jsonify({"error": f"The folder path does not exist: {folder_path}"}), 400
file_names = os.listdir(folder_path)
return jsonify({"files": file_names})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Dockerfile
FROM python:3.8-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
requirements.txt
blinker==1.8.2
click==8.1.7
Flask==3.0.3
itsdangerous==2.2.0
Jinja2==3.1.4
MarkupSafe==2.1.5
Werkzeug==3.0.3
compose.yaml
version: '3.8'
services:
app:
build: .
volumes:
- /mnt/c:/mnt/c
ports:
- "5000:5000"
Steps Taken:
- Verified project structure and contents of each file.
- Ran the Flask app directly in WSL without Docker:
It successfully lists the files in the specified folder.
I tested with Postman: - URL: http://localhost:5000/list_files
- Method: POST
- Body: raw and JSON
{
“folder_path”: “C:UsersAnyUsernameAnyFolder”
} - Error Encountered when sending request on Postman
When running inside Docker, the application responds with:
{
“error”: “The folder path does not exist: /mnt/c/Users/AnyUsername/AnyFolder”
}
Additional Information:
- WSL Version: WSL 2
- Docker Version: 4.31.1
- OS: Windows 11
Again, everything works when running on my local machine, but breaks after dockerizing. Any help would be greatly appreciated, thanks in advance.