简体   繁体   中英

Cloud builds failing for super simple Flask app no matter what I do

So, I wanted to try GCloud since you can deploy serverless stuff pretty easily. I've made a simple Flask app to test it out, this is the entire code for the app:

from flask import (
    Flask
)
from flask_cors import CORS

app = Flask(__name__)
cors = CORS(app)

@app.route('/ping', methods=['GET'])
def ping():
    return 'It works!', 200

def create_app():
    return app

if __name__ == '__main__':
    app.run()

Here is the Dockerfile:

FROM python:3.7-slim

COPY . ./home/gcloud-test

WORKDIR /home/gcloud-test

RUN pip install -r requirements.txt

EXPOSE 5000

CMD python3 main.py

I've also tried using gunicorn and waitress to start the server, the same thing happens.

Commands I run to deploy to gcloud:

gcloud builds submit --tag gcr.io/PROJECT_ID/PROJECT_NAME

gcloud run deploy --image gcr.io/PROJECT_ID/PROJECT_NAME --platform managed --verbosity=debug

Here is the stack trace from the console:

Deploying container to Cloud Run service [PROJECT_NAME] in project [PROJECT_ID] region [europe-west1]
Deploying...
  Creating Revision... Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information....failed
Deployment failed
DEBUG: (gcloud.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.
Traceback (most recent call last):
    resources = calliope_command.Run(cli=self, args=args)
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\calliope\backend.py", line 808, in Run
    resources = command_instance.Run(args)
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\surface\run\deploy.py", line 219, in Run
    build_log_url=build_log_url)
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\command_lib\run\serverless_operations.py", line 1087, in ReleaseService
    self.WaitForCondition(poller)
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\command_lib\run\serverless_operations.py", line 594, in WaitForCondition
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\api_lib\util\waiter.py", line 326, in PollUntilDone
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\util\retry.py", line 219, in RetryOnResult
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\command_lib\run\serverless_operations.py", line 251, in Poll
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\command_lib\run\serverless_operations.py", line 230, in _PollTerminalSubconditions
    self._PossiblyFailStage(condition, message)
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\command_lib\run\serverless_operations.py", line 349, in _PossiblyFailStage
    message)
  File "C:\Users\aleks\AppData\Local\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\console\progress_tracker.py", line 915, in FailStage
    raise failure_exception  # pylint: disable=raising-bad-type
googlecloudsdk.command_lib.run.exceptions.DeploymentFailedError: Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.
ERROR: (gcloud.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.

When I go to see the logs online, I'm seeing that the app has been started, and a couple of minutes later health check fails, screenshot attached:

日志的屏幕截图

Any kind of help would be appreciated.

You configured your container (and Flask app) to listen on port 5000, but the cloud run container contract says that you need to listen on port 8080 (available as the PORT environment variable).

The container must listen for requests on 0.0.0.0 on the port to which requests are sent. By default, requests are sent to 8080, but you can configure Cloud Run to send requests to the port of your choice.

As the logs show, the healthcheck is failing as a result.

You can specify the port when you deploy, as seen here .

For example:

gcloud run deploy --image gcr.io/PROJECT_ID/PROJECT_NAME --platform managed --port 5000 --verbosity=debug

This is not quite enough however.

Flask.run()defaults are not to listen on 0.0.0.0 (and instead the default is 127.0.0.1). Your logs indeed confirm the server is listening on http://127.0.0.1:5000 . You you will need to specify that as a host to listen on when you call run() .


I was able to replicate your problem with your code as written. I changed the app startup line to this:

if __name__ == '__main__':
    app.run(host='0.0.0.0')

Rebuilt, and deployed specifying the port as 5000 as seen in the above deploy command, and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM