Cloud Run Deploy fails: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable

Brian C

I have a project which I had previously successfully deployed to Google Cloud Run, and set up with a trigger such that upon pushing to the repo's main branch on Github, it would automatically deploy. It worked great.

Then I tried to rename the github repo, which meant deleting and creating a new trigger, and now I cannot get it working again.

Everytime, the build succeeds but deployment fails with this error in Cloud Build:

Step #2 - "Deploy": ERROR: (gcloud.run.services.update) 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.

I have not changed anything other than the repo name, leading me to believe the fix is not with my code, but I tried some changes there anyway.

I have looked into the solutions set forth in this post. However, I believe I am listening on the correct port.

My app is using Python and Flask, and contains this:

if __name__ == "__main__":
    app.run(debug=False, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

Which should use the ENV var Port (8080) and otherwise default to 8080. I also tried just using port=8080.

I tried explicitly exposing the port in the Dockerfile, which also did not work:

FROM python:3.7
   
#Copy files into docker image dir, and make that the current working dir
COPY . /docker-image
WORKDIR /docker-image

RUN pip install -r requirements.txt

CMD ["flask", "run", "--host", "0.0.0.0"]

EXPOSE 8080

Cloud Run does seem to be using port 8080 - if I dig into the response, I see this nested under Response.spec.container.0 :

ports: [
  0: {
    containerPort: 8080
    name: "http1"
  }
]

All that said, if I look at the logs, it shows "Now running on Port 5000".

enter image description here

I have no idea where that Port 5000 is coming from or being set, but trying to change the ports in Python/Flask and the Dockerfile to 5000 leads to the same errors.

How do I get it to run on Port 8080? It's very strange to me that this was working FINE prior to renaming the repo and creating a new trigger. How is this setup different? The Trigger does not give an option to set the port so I'm not sure how that caused this error.

guillaume blaquiere

You have mixed things. Flask command default port is effectively 5000. If you want to change it, you need to change your flask run command with the --port= parameter

CMD ["flask", "run", "--host", "0.0.0.0","--port","8080"]

In addition, your flask run command, is a flask runtime and totally ignore the standard python entrypoint if __name__ == "__main__":. If you want to use this entrypoint, use the Python runtime

CMD ["python", "<main file>.py"]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

The Tomcat connector configured to listen on port 8080 failed to start

Spring Boot application in eclipse, the Tomcat connector configured to listen on port XXXX failed to start

Spring Boot application in eclipse, the Tomcat connector configured to listen on port XXXX failed to start

Spring boot after https: The Tomcat connector configured to listen on port 8444 failed to start.

container start up failed in jenkins

The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured

The Tomcat connector configured to listen on port 8080 failed

docker postgres failed to start with specified port

An exception occurred while running. null: InvocationTargetException: Connector configured to listen on port 8080 failed to start -> [Help 1]

Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable

Cloud run failed to start because of invalid SQL Cloud configuration

Cloud Run error: Container failed to start

Google Cloud Run fails to listen even after changing port to 8080

Cloud Run service in GCP shows "Container failed to start and then listen on the port defined by the PORT environment variable" Codeigniter 4

Tishadow failed to start server on port 3000

Need to listen on a port and then start a program

Openshift -> node js , failed to start port 8080

Remote port forwarding failed to listen on any port

Failed to start ipmiutil ipmi_port service

Nginx container fails to start on Cloud Run

Start sonarqube on port specified in environment variable instead of 9000

IBM MQ container failed to start

Cloud Run error: Container failed to start. Failed to start and then listen on the port

"Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable."

apache-jmeter-5.4.1 Server failed to start: java.rmi.server.ExportException: Listen failed on port 4000

web server failed to start port 7081 was already in use

The user-provided container failed to start and listen on the port defined provided by the PORT=8080

(gcloud.run.deploy) The user-provided container failed to start and listen

container failed to start