Here are my theories:
- localhost (
127.0.0.1
) is being used; should use 0.0.0.0
- Flask internal WSGI server is being used; should use e.g. Gunicorn
NB You may develop and test these solutions using Cloud Shell. Cloud Shell (now) includes a web preview feature that permits browsing endpoints (including :8080
) for servers running on the Cloud Shell instance.
Flask
Flask includes a development (WSGI) server and tutorials generally include:
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
Which, if run as python somefile.py
will use Flask's inbuilt (dev) server and expose it on localhost (127.0.0.1
).
This is inaccessible from other machines:
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 244-629-469
If instead, host='0.0.0.0'
is used, then this will work:
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 244-629-469
192.168.9.1 - - [08/May/2019 23:59:59] "GET / HTTP/1.1" 200 -
192.168.9.1 - - [08/May/2019 23:59:59] "GET /favicon.ico HTTP/1.1" 404 -
E.g. Gunicorn
Flask's inbuilt server should not be used and Flex's documentation describes how to use gunicorn (one of various alternatives) should be configured:
https://cloud.google.com/appengine/docs/flexible/python/runtime#application_startup
Which, if run gunicorn --bind=0.0.0.0:8080 main:app
gives:
[INFO] Starting gunicorn 19.9.0
[INFO] Listening at: http://0.0.0.0:8080 (1)
[INFO] Using worker: sync
[INFO] Booting worker with pid: 7
App Engine Flex
Using the recommended configuration, app.yaml would include:
runtime: python
env: flex
entrypoint: gunicorn --bind:$PORT main:app
Dockerfiles
You can test these locally with Dockerfiles and -- if you wish -- deploy these to Flex as custom runtimes (after revising app.yaml
):
FROM python:3.7-alpine
WORKDIR /app
ADD . .
RUN pip install -r requirements.txt
For Flask add:
ENTRYPOINT ["python","main.py"]
NB In the above, the configuration results from the somefile.py app.run(...)
And for gunicorn:
ENTRYPOINT ["gunicorn","--bind=0.0.0.0:8080","main:app"]