简体   繁体   English

Github 到 Google Cloud Run:Python Flask API 在 Google Cloud Run 上运行的 WSGI 给出“容器无法启动并侦听端口”

[英]Github to Google Cloud Run: Python Flask API WSGI running on Google Cloud Run gives "Container failed to start and listen on the port"

I am trying to create a Python Flask API on WSGI that I want to run on Google Cloud Run.我正在尝试在我想在 Google Cloud Run 上运行Python Flask API on WSGI

The code is stored in a Github repository.代码存储在 Github 存储库中。 In https://console.cloud.google.com/run I have added the Github repository so that when a new push is made to main branch then a build will be done.https://console.cloud.google.com/run我添加了 Github 存储库,这样当对主分支进行新的推送时,就会完成构建。

The code is a Flask API running on WSGI webserver.代码是运行在 WSGI 网络服务器上的 Flask API。 I get some error messages when I try to run it on Google Run: ERROR: build step 2 "gcr.io/google.com/cloudsdktool/cloud-sdk:slim" failed: step exited with non-zero status: 1当我尝试在 Google Run 上运行它时收到一些错误消息: ERROR: build step 2 "gcr.io/google.com/cloudsdktool/cloud-sdk:slim" failed: step exited with non-zero status: 1

config/flask-site-nginx.conf配置/flask-site-nginx.conf

server {
    location / {
        try_files $uri @yourapplication;
    }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
}

config/nginx.conf配置/nginx.conf

# based on default config of nginx 1.12.1
# Define the user that will own and run the Nginx server
user nginx;
# Define the number of worker processes; recommended value is the number of
# cores that are being used by your server
# auto will default to number of vcpus/cores
worker_processes auto;

# altering default pid file location
pid /tmp/nginx.pid;

# turn off daemon mode to be watched by supervisord
daemon off;

# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;

# Define the location on the file system of the error log, plus the minimum
# severity to log messages for
error_log /var/log/nginx/error.log warn;

# events block defines the parameters that affect connection processing.
events {
    # Define the maximum number of simultaneous connections that can be opened by a worker process
    worker_connections  1024;
}


# http block defines the parameters for how NGINX should handle HTTP web traffic
http {
    # Include the file defining the list of file types that are supported by NGINX
    include /etc/nginx/mime.types;
    # Define the default file type that is returned to the user
    default_type text/html;

    # Don't tell nginx version to clients.
    server_tokens off;

    # Specifies the maximum accepted body size of a client request, as
    # indicated by the request header Content-Length. If the stated content
    # length is greater than this size, then the client receives the HTTP
    # error code 413. Set to 0 to disable.
    client_max_body_size 0;

    # Define the format of log messages.
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

    # Define the location of the log of access attempts to NGINX
    # access_log /var/log/nginx/access.log  main;
    access_log /dev/stdout main;

    # Define the parameters to optimize the delivery of static content
    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;

    # Define the timeout value for keep-alive connections with the client
    keepalive_timeout  65;

    # Define the usage of the gzip compression algorithm to reduce the amount of _data to transmit
    #gzip  on;

    # Include additional parameters for virtual host(s)/server(s)
    include /etc/nginx/conf.d/*.conf;
}

supervisord.conf supervisord.conf文件

[supervisord]
nodaemon=true

[program:uwsgi]
command=/usr/local/bin/uwsgi --ini /etc/uwsgi/uwsgi.ini --die-on-term
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:nginx]
command=/usr/sbin/nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

uwsgi.ini uwsgi.ini文件

[uwsgi]
module = src.wsgi
callable = app

uid = nginx
gid = nginx

socket = /tmp/uwsgi.sock
chown-socket = nginx:nginx
chmod-socket = 664

cheaper = 1
processes = %(%k + 1)

src/ __init__ .py源代码/ __init__ .py

(empty file)

src/main.py源代码/main.py

import os

from flask import Flask
from flask_cors import CORS

app = Flask(__name__)

cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'


# Routes
@app.route('/', methods=['GET'])
def __index():
    return "Hello from Flask API running on WSGI"


# - Main start ----------------------------------------------------------------
if __name__ == "__main__":
    # Start app
    print("main() :: Flask API is starting at: http://127.0.0.1:8080")
    app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

src/wsgi.py源代码/wsgi.py

import os

from src.main import app

if __name__ == "__main__":
    print("wsgi() :: Flask API is starting at: http://127.0.0.1:8080")
    app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

Dockerfile Dockerfile

# Specify Python
FROM python:latest

# Copy files
RUN mkdir /app
WORKDIR /app
COPY . .

# Install Nginx web server
RUN apt-get update
RUN apt-get install -y --no-install-recommends \
        libatlas-base-dev gfortran nginx supervisor

# Install uWSGI Web Server Gateway Interface
RUN pip install uwsgi

# Install requiremets
RUN pip install -r requirements.txt

# Configure Nginx and uWSGI
RUN useradd --no-create-home nginx

RUN rm /etc/nginx/sites-enabled/default
RUN rm -r /root/.cache

COPY config/nginx.conf /etc/nginx/
COPY config/flask-site-nginx.conf /etc/nginx/conf.d/
COPY config/uwsgi.ini /etc/uwsgi/
COPY config/supervisord.conf /etc/

CMD ["/usr/bin/supervisord"]

requirements.txt要求.txt

flask
flask-unittest
Flask-RESTful
flask-cors

When I try to push the code Google Run builds it but it gives me errors:当我尝试推送代码时,Google Run 构建了它,但它给了我错误:

Creating revision:创建修订:

The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable.用户提供的容器无法启动并侦听由 PORT=8080 环境变量提供的端口。 Logs for this revision might contain more information.此修订的日志可能包含更多信息。 Logs URL: https://console.cloud.google.com/logs/viewer?project=engineering-infra&resource=cloud_run_revision/service_name/template-google-cloud-run-backend/revision_name/template-google-cloud-run-backend-00009-rew&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22template-google-cloud-run-backend%22%0Aresource.labels.revision_name%3D%22template-google-cloud-run-backend-00009-rew%22 For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start日志 URL: https://console.cloud.google.com/logs/viewer?project=engineering-infra&resource=cloud_run_revision/service_name/template-google-cloud-run-backend/revision_name/template-google-cloud-run-backend -00009-rew&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22template-google-cloud-run-backend%22%0Aresource.labels.revision_name%3D%22template-google-cloud-run -backend-00009-rew%22有关更多故障排除指南,请参阅https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start

Routing traffic路由流量

Pending待办的

Building and deploying from repository:从存储库构建和部署:

Trigger execution failed: source code could not be built or deployed;触发器执行失败:无法构建或部署源代码; find more information in build logs Revision 'template-google-cloud-run-backend-00009-rew' is not ready and cannot serve traffic.在构建日志中查找更多信息修订版“template-google-cloud-run-backend-00009-rew”尚未就绪,无法提供流量。 The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable.用户提供的容器无法启动并侦听由 PORT=8080 环境变量提供的端口。 Logs for this revision might contain more information.此修订的日志可能包含更多信息。 Logs URL: https://console.cloud.google.com/logs/viewer?project=engineering-infra&resource=cloud_run_revision/service_name/template-google-cloud-run-backend/revision_name/template-google-cloud-run-backend-00009-rew&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22template-google-cloud-run-backend%22%0Aresource.labels.revision_name%3D%22template-google-cloud-run-backend-00009-rew%22 For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start日志 URL: https://console.cloud.google.com/logs/viewer?project=engineering-infra&resource=cloud_run_revision/service_name/template-google-cloud-run-backend/revision_name/template-google-cloud-run-backend -00009-rew&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22template-google-cloud-run-backend%22%0Aresource.labels.revision_name%3D%22template-google-cloud-run -backend-00009-rew%22有关更多故障排除指南,请参阅https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start

The build logs gives me:构建日志给了我:

Step #2 - "Deploy": 219f975b51dc: Pull complete
Step #2 - "Deploy": Digest: sha256:8dde46b24ab5c496d1fb11bdd87a68a07f8d752098caac87bc60d59d904a1ff5
Step #2 - "Deploy": Status: Downloaded newer image for gcr.io/google.com/cloudsdktool/cloud-sdk:slim
Step #2 - "Deploy": gcr.io/google.com/cloudsdktool/cloud-sdk:slim
Step #2 - "Deploy": Deploying...
Step #2 - "Deploy": Creating Revision....failed
Step #2 - "Deploy": Deployment failed
Step #2 - "Deploy": ERROR: (gcloud.run.services.update) The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.
Step #2 - "Deploy": 
Step #2 - "Deploy": Logs URL: https://console.cloud.google.com/logs/viewer?project=engineering-infra&resource=cloud_run_revision/service_name/template-google-cloud-run-backend/revision_name/template-google-cloud-run-backend-00009-rew&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22template-google-cloud-run-backend%22%0Aresource.labels.revision_name%3D%22template-google-cloud-run-backend-00009-rew%22 
Step #2 - "Deploy": For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start
Finished Step #2 - "Deploy"
ERROR
ERROR: build step 2 "gcr.io/google.com/cloudsdktool/cloud-sdk:slim" failed: step exited with non-zero status: 1

Firstly,the error that is seen during the build execution step clearly states that the revision for the build is not ready on the path, which could be due to the build not ready or the process not recognizing this build due to tag issue.首先,在构建执行步骤中看到的错误清楚地指出构建的修订版未在路径上准备好,这可能是由于构建未就绪或由于标记问题导致进程无法识别此构建。
Trigger execution failed: source code could not be built or deployed; find more information in build logs Revision 'template-google-cloud-run-backend-00009-rew' is not ready and cannot serve traffic
To prevent this issue,ensure that the revision build is ready to be picked and try again.Also try to remove that “revision”tag and try again.为防止出现此问题,请确保已准备好选择修订版本并重试。同时尝试删除该“修订”标签并重试。
These changes will help because when updating traffic, "0%" traffic was being assigned to every revision specified in the traffic field and as of now since this revision is not ready, any operation that is assigning traffic (even though 0%) would cause the error.这些更改将有所帮助,因为在更新流量时,“0%”流量被分配给流量字段中指定的每个修订版,截至目前,由于该修订版尚未准备就绪,任何分配流量的操作(即使是 0%)都会导致错误。
Secondly, the listener port error suggests that when your container starts, it expects a response on port $PORT (8080),as per the environment variable is set.Now as the build is not ready it does not follow the Cloud Run requirements and is terminated when Cloud Run detects no response / incorrect response on port $PORT.其次,侦听器端口错误表明,当您的容器启动时,根据设置的环境变量,它期望端口 $PORT (8080) 上的响应。现在由于构建尚未准备好,它不符合 Cloud Run 要求并且是当 Cloud Run 在端口 $PORT 上检测到无响应/不正确响应时终止。
Read this document to better understand the runtime environment contract .阅读本文档以更好地理解 运行时环境契约

Please review this document which lists a series of steps to follow when encountering this error in order to fix the issue.请查看此文档,其中列出了遇到此错误时要遵循的一系列步骤以解决问题。
Also have a look at these similar examples:也看看这些类似的例子:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud Run - 容器无法启动解决方法 - Google Cloud Run - Container failed to start workarounds “Cloud Run 错误:容器启动失败。无法启动然后侦听 PORT 环境变量定义的端口。” - "Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable." Cloud Run:用户提供的容器启动失败,监听PORT=8080提供的端口 - Cloud Run: The user-provided container failed to start and listen on the port defined provided by the PORT=8080 Cloud Run 错误:容器无法启动。 无法启动然后侦听 PORT 环境变量定义的端口。 - Django - Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. - Django 谷歌云运行端口 - Google Cloud Run Port 谷歌云,GitHub pipe 与谷歌云运行 - Google Cloud, GitHub pipe with Google Cloud Run Cloud Run Flask API 容器运行shutit进入休眠循环 - Cloud Run Flask API container running shutit enters a sleep loop Google Cloud Run 中 Flask 应用程序的 Celery 任务 - Celery Tasks for Flask App in Google Cloud Run Google Cloud Run 上 API 服务器的健康检查 - Health checks for an API server on Google Cloud Run Google Cloud Run with Angular 通用监听端口错误 - Google Cloud Run with Angular Universal listening port error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM