无论我做什么,云构建都无法构建超级简单的 Flask 应用程序
Cloud builds failing for super simple Flask app no matter what I do
所以,我想试试 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.
上网查看日志,发现应用已经启动,几分钟后健康检查失败,截图如下:
如有任何帮助,我们将不胜感激。
您将容器(和 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,它成功了。
所以,我想试试 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.
上网查看日志,发现应用已经启动,几分钟后健康检查失败,截图如下:
如有任何帮助,我们将不胜感激。
您将容器(和 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,它成功了。