如何解决将docker映像部署到云运行时出现的“容器启动失败错误”

2024-10-04 09:29:39 发布

您现在位置:Python中文网/ 问答频道 /正文

问题陈述:我创建了一个docker应用程序(一个简单的python代码),并尝试使用Google Cloudrun部署和自动运行,但每次在部署过程中我都会看到以下错误:

命令:

gcloud run deploy my_service_name --image gcr.io/project-id/image_name

错误

 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 info
rmation.                                                                                                                                                                                                   
  X 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 inform
  ation.                                                                                                                                                                                                   
  . Routing traffic...                                                                                                                                                                                     
Deployment failed            

我的Dockerfile

#FROM python:3.8
FROM python:latest
# Copy application dependency manifests to the container image.
COPY requirements.txt ./
# Install production dependencies.
RUN pip install -r requirements.txt
# Copy main 
COPY main.py ./
ENV PORT = 8080
CMD [ "python", "./main.py" ]

在我的python程序中,仅仅运行一系列sql查询并更新BQ表并没有什么特别之处

我使用了Google列出的troubleshooting steps,容器在我的本地计算机上运行,没有任何问题

有人能告诉我哪里出了问题吗

编辑:将Dockerfile更新为ENV PORT=8080(删除了空格)。仍然收到相同的错误。下面是部署日志

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):
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 983, in Execute
    resources = calliope_command.Run(cli=self, args=args)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/calliope/backend.py", line 807, in Run
    resources = command_instance.Run(args)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/surface/run/deploy.py", line 258, in Run
    build_log_url=build_log_url)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/command_lib/run/serverless_operations.py", line 1087, in ReleaseService
    self.WaitForCondition(poller)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/command_lib/run/serverless_operations.py", line 594, in WaitForCondition
    return waiter.PollUntilDone(poller, None, wait_ceiling_ms=1000)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/api_lib/util/waiter.py", line 326, in PollUntilDone
    sleep_ms=sleep_ms)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/core/util/retry.py", line 219, in RetryOnResult
    result = func(*args, **kwargs)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/command_lib/run/serverless_operations.py", line 251, in Poll
    self._PollTerminalSubconditions(conditions, conditions_message)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/command_lib/run/serverless_operations.py", line 230, in _PollTerminalSubconditions
    self._PossiblyFailStage(condition, message)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/command_lib/run/serverless_operations.py", line 349, in _PossiblyFailStage
    message)
  File "/Users/analyst045/Documents/google-cloud-sdk/lib/googlecloudsdk/core/console/progress_tracker.py", line 915, in FailStage
    raise failure_exception  # pylint: disable=raising-bad-type
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.

日志如下:

{
 insertId: "5ec6519000ca44b34efd"  
 labels: {
  instanceId: "00bf4bf02dd520b9906e825b7333299aac62e9e4f12e4090ccd3b3bfd2435203c13fbe3c09193bd6dd408286adf939dff830"   
 }
 logName: "projects/project-name/logs/run.googleapis.com%2Fvarlog%2Fsystem"  
 receiveTimestamp: "2020-05-18T10:46:09.076605834Z"  
 resource: {
  labels: {
   configuration_name: "measurement-protocol-hit"    
   location: "us-central1"    
   project_id: "project-id"    
   revision_name: "measurement-protocol-hit-00001-det"    
   service_name: "measurement-protocol-hit"    
  }
  type: "cloud_run_revision"   
 }
 severity: "WARNING"  
 textPayload: "Container called exit(0)."  
 timestamp: "2020-05-18T10:36:09.068639979Z"  
}

Tags: thetoruninpycloudlibgoogle
2条回答

基于Dockerfile reference,可以通过以下两种方式之一指定端口:

ENV <key> <value>
ENV <key>=<value> ...

因为=周围有空格,Docker认为您使用的是第一种形式,其中键是PORT,值是= 8000(这不是一个数字,可能是Cloud Run不起作用的原因)

要修复它,请指定不带空格(ENV PORT=8000)或不带等号(ENV PORT 8000)的端口

您不应该这样做:

ENV PORT = 8080

不仅如@supersam654所说,这是错误的语法,PORT环境变量将通过Cloud Run传递给您的应用程序–您自己不会这样传递它

正如错误多次指出的(您发布的不是“详细日志”),您应该:

Logs for this revision might contain more information.

因此,请转到https://console.cloud.google.com/run并获取此应用程序的日志

您可能应该确保在部署之前在本地测试您的容器。可以在此处找到说明:https://cloud.google.com/run/docs/testing/local

相关问题 更多 >