gpt4 book ai didi

python - 无论我做什么, super 简单的 Flask 应用程序的云构建都失败了

转载 作者:行者123 更新时间:2023-12-03 16:59:24 30 4
gpt4 key购买 nike

所以,我想尝试 GCloud,因为你可以很容易地部署无服务器的东西。我制作了一个简单的 Flask 应用程序来测试它,这是该应用程序的完整代码:

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()
这是 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
我也试过使用 gunicornwaitress启动服务器,同样的事情发生。
我运行以部署到 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


这是来自控制台的堆栈跟踪:
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.
当我去在线查看日志时,我看到应用程序已经启动,几分钟后健康检查失败,附上截图:
Screenshot of the logs
任何形式的帮助将不胜感激。

最佳答案

您将容器(和 Flask 应用程序)配置为监听端口 5000,但是 cloud run container contract表示您需要监听端口 8080(可作为 PORT 环境变量使用)。

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.


如日志所示,运行状况检查因此失败。
部署时可以指定端口,见 here .
例如:
gcloud run deploy --image gcr.io/PROJECT_ID/PROJECT_NAME --platform managed --port 5000 --verbosity=debug

然而,这还不够。 Flask.run() defaults不要听 0.0.0.0 (而默认值为 127.0.0.1)。您的日志确实确认服务器正在监听 http://127.0.0.1:5000 .您需要将其指定为当您调用 run() 时要监听的主机。 .

我能够用您编写的代码复制您的问题。我将应用程序启动行更改为:
if __name__ == '__main__':
app.run(host='0.0.0.0')
如上面的部署命令所示,重新构建并部署将端口指定为 5000,并且它可以工作。

关于python - 无论我做什么, super 简单的 Flask 应用程序的云构建都失败了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63141482/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com