Flask App Not Binding to Port Defined by PORT Environment Variable

Hello,

I’m having trouble getting my Flask app to bind to the port defined by the PORT environment variable on Render. Here are the steps I’ve taken:

  1. Flask App Code: I’ve updated my Flask app to read the HOST and PORT environment variables and bind to 0.0.0.0 and 10000 accordingly. Below is the relevant code snippet from my app.py:
if __name__ == "__main__":
    app.run(host=os.getenv('HOST'), port=int(os.getenv('PORT'),8080), debug=False)

And here are my .env variables:

PORT = 10000
HOST = 0.0.0.0

Additional Details:

  • The app works correctly when running locally and binds to the specified port.
  • I’ve tried different port values and ensured that host='0.0.0.0' is set in the app.run() method.
  • I have also set HOST (0.0.0.0) and PORT (10000) as environment variables on Render’s UI

Despite these steps, my app does not seem to bind to the port defined by the PORT environment variable when deployed on Render. The app works locally, but deployment fails on Render with the following error in the logs:

==> No open ports detected, continuing to scan...
==> Docs on specifying a port: https://render.com/docs/web-services#port-binding
==> Port scan timeout reached, no open ports detected. Bind your service to at least one port. If you don't need to receive traffic on any port, create a background worker instead.

Addition:

On my latest attempt it also did this:

2024-07-06 13:37:49,485 - INFO - Commands set successfully
[2024-07-06 13:37:49 +0000] [89] [INFO] Starting gunicorn 22.0.0
[2024-07-06 13:37:49 +0000] [89] [INFO] Listening at: http://0.0.0.0:10000 (89)
[2024-07-06 13:37:49 +0000] [89] [INFO] Using worker: sync
[2024-07-06 13:37:49 +0000] [122] [INFO] Booting worker with pid: 122
127.0.0.1 - - [06/Jul/2024:13:37:49 +0000] "HEAD / HTTP/1.1" 404 0 "-" "Go-http-client/1.1"
127.0.0.1 - - [06/Jul/2024:13:37:49 +0000] "OPTIONS / HTTP/1.1" 404 232 "-" "Go-http-client/1.1"
==> Your service is live 🎉

which I am not sure if is correct or not…? the deployment seems to have succeded but I am not sure since the server seems to be running on 127.0.0.1 instead of 0.0.0.0

also, I am unable to upgrade to a paid membership…? every time I try to purchase it, it just resets my deployment…?

Hi,

I’ve replied to the ticket you also opened. Let’s keep the conversation in one place (on the ticket). Then you can update this post when we get to the solution.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.