Search code examples
pythonauthenticationflaskgoogle-cloud-platformgoogle-cloud-run

Flask app cannot run on google cloud run: service unavailable 503 error


I created a web app with Flask that exposes the 8080 port and shows some html pages, and I deployed it with google cloud run. This is the basic code.

app = Flask(__name__)

@app.before_request
def before_request():
    session.permanent = True
    app.permanent_session_lifetime = timedelta(minutes=settings.session_lifetime_minutes)
    session.modified = True
    g.user = current_user

login_manager = LoginManager()
login_manager.init_app(app)


@app.route("/")
def index():
    return render_template('authentication.html')

However when I set up the ingress of the service available to everybody and I access the URL provided by Google (https://myproject.run.app) through the browser, I wait for 5/10 seconds and get 'Service unavailable'. By inspecting the logs I see this error log:

severity: "ERROR"
textPayload: "The request failed because either the HTTP response was malformed or connection to the instance had an error. Additional troubleshooting documentation can be found at: https://cloud.google.com/run/docs/troubleshooting#malformed-response-or-connection-error"

However in the troubleshooting guide I found no useful information.

When I setup the ingress of the service available only to internal users and I try to use the curl suggested by google, I get something like:

<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>404 Page not found</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Page not found</h1>
<h2>The requested URL was not found on this server.</h2>
<h2></h2>
</body></html>

Can anyone help me to understand what's going on? Thanks!


Solution

  • In your comment:

    if name == "main":
        app.run(ssl_context="adhoc", host="0.0.0.0", port=8080)
    

    This is your probem ssl_context="adhoc". The listener must use HTTP and not HTTPS.

    if name == "main":
        app.run(host="0.0.0.0", port=8080)
    

    Suggestion: read the PORT value from the environment instead of hard coding.

    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))