Skip to content
Advertisement

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.

The code is stored in a Github repository. 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.

The code is a Flask API running on WSGI webserver. 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

config/flask-site-nginx.conf

JavaScript

config/nginx.conf

JavaScript

supervisord.conf

JavaScript

uwsgi.ini

JavaScript

src/__init__.py

JavaScript

src/main.py

JavaScript

src/wsgi.py

JavaScript

Dockerfile

JavaScript

requirements.txt

JavaScript

When I try to push the code Google Run builds it but it gives me errors:

Creating revision:

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. 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

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. 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. 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

The build logs gives me:

JavaScript

Advertisement

Answer

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.
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.
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:

Advertisement