1

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.

Brian C
  • 1,333
  • 3
  • 19
  • 36
  • Show the command or build step that deployed the Cloud Run service. You can specify the port number via the gcloud command line option **--port=8080**. – John Hanley Sep 15 '21 at 03:47
  • It kicks off automatically when I push to githhub, so I don't specify it like that. I confirmed it was at port 8080 based on this: https://cloud.google.com/run/docs/configuring/containers#console However It's possible that setting is not used when you are doing github integration for deployment – Brian C Sep 15 '21 at 04:03
  • If you configured the deploy action in the Console GUI, then you also configured the port number. Your documentation link shows that step. https://cloud.google.com/run/docs/configuring/containers#console – John Hanley Sep 15 '21 at 04:06

1 Answers1

3

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"]
guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
  • I went the second route, changing the Dockerfile to point to main.py, and now it works! Thanks! What I still find curious is, as near as I can tell, the previous setup used to workup with the old repo - I think it somehow ignored the CMD ["flask"...] and used the python anyway... but I did have both "mixed" methods previously when it was working. Odd! – Brian C Sep 15 '21 at 19:17
  • As a followup, is there a particular advantage/disadvantage to using one method over the other? Is there a particular reason I might want to change it to have Docker run flask instead? – Brian C Sep 15 '21 at 19:18
  • 2
    Sadly, I'm not a python expert, and I don't know the difference between both. It could be another great question! – guillaume blaquiere Sep 15 '21 at 19:38