I am deploying a Flask application to Google App Engine Standard Environment, which serves a React frontend built with Webpack. The build process completes without errors, and the dist directory is correctly populated (confirmed via Docker build logs). However, when deployed to GAE, the Flask application logs an error indicating that the dist directory is not found or is empty.
Here are the relevant parts of my setup:
Dockerfile (partial):
# Node.js build stage
FROM node:20 as build
WORKDIR /app/frontend
# ... (copying package.json and files)
RUN npm run build
RUN ls -l dist
# Python runtime stage
FROM python:3.8
WORKDIR /app
COPY --from=build /app/frontend/dist ./frontend/dist
RUN ls -l frontend/dist
# ... (rest of the Dockerfile)
CMD ["sh", "-c", "ls -al frontend/dist && exec gunicorn -b :$PORT api.app:app --log-level=debug"]
app.yaml (partial):
runtime: python38
service: default
env_variables: ***
entrypoint: gunicorn -b :$PORT api.app:app --log-level=debug
automatic_scaling:
target_cpu_utilization: 0.65
min_instances: 1
max_instances: 15
min_idle_instances: automatic
max_idle_instances: automatic
Flask app:
app = Flask(__name__, static_folder='/app/frontend/dist')
...
@app.route('/<path:path>')
def catch_all(path):
try:
# If the path does not start with 'api/' and is not a file, serve index.html
if not path.startswith('api/') and '.' not in path:
return app.send_static_file('index.html')
return send_from_directory(app.static_folder, path)
except Exception as e:
# Log any exceptions that occur during the request
logger.error(f"Error serving '{path}': {str(e)}")
return jsonify(error="The requested URL was not found. - catch_all"), 404
When running the Docker container locally, the dist directory is present and contains the React build files. However, upon deployment, the Flask app's error logs show:
ERROR - {'error': 'The resource was not found', 'dist_directory': '/app/frontend/dist', 'dist_contents': []}
The .gcloudignore does not exclude the dist directory, and the GAE logs do not indicate any issues during deployment.
Could this be an issue with how static files are handled in the GAE Standard Environment, or is there a misconfiguration in my setup? Any advice on troubleshooting this would be greatly appreciated.
Hi,
Are you saying you did a build of your React App locally and directed its output to a folder in a Flask Application (directory). After this, you then deployed your Flask Application using gcloud app deploy? If the above summary is incorrect, can you explain what you did?
Hi! Thank you for the reply. I built the app locally to develop and test it, then I built it in App Engine using GitHub repository trigger.
Hi @Josefk,
Welcome to Google Cloud Community!
Make sure that the resource path is correct as the error suggests that it can't find the correct resource path. You also need to check if Flask framework is also added in your requirements.txt
file. It should look like this:
Flask==3.0.0
You may want to check the following documentations as you may have missed out some steps while deploying your Google App Engine:
If the aforementioned steps did not work, you may file a bug through this Google Cloud issue tracker so that our engineers could take a look at it. We don't have a specific ETA but you can keep track of its progress once the ticket has been created.
Hope this helps.