无论我做啥,超级简单的 Flask 应用程序的云构建都失败了
Posted
技术标签:
【中文标题】无论我做啥,超级简单的 Flask 应用程序的云构建都失败了【英文标题】:Cloud builds failing for super simple Flask app no matter what I do无论我做什么,超级简单的 Flask 应用程序的云构建都失败了 【发布时间】:2020-11-18 07:52:46 【问题描述】:所以,我想尝试 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
我也尝试过使用gunicorn
和waitress
来启动服务器,同样的事情发生了。
我运行以部署到 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.
当我去网上查看日志时,我看到应用程序已经启动,几分钟后健康检查失败,附上截图:
我们将不胜感激。
【问题讨论】:
Cloud Run 默认使用 8080 端口。将您的代码更改为以下内容:app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
【参考方案1】:
您将容器(和 Flask 应用)配置为侦听端口 5000,但 cloud run container contract 表示您需要侦听端口 8080(可作为 PORT
环境变量使用)。
容器必须在请求发送到的端口上侦听 0.0.0.0 上的请求。默认情况下,请求会发送到 8080,但您可以将 Cloud Run 配置为将请求发送到您选择的端口。
如日志所示,运行状况检查因此失败。
部署的时候可以指定端口,见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 重新构建和部署,并且成功了。
【讨论】:
以上是关于无论我做啥,超级简单的 Flask 应用程序的云构建都失败了的主要内容,如果未能解决你的问题,请参考以下文章