Shannon Rothe
Shannon Rothe

Reputation: 1122

Deploying Flask with Heroku

I'm trying to deploy a Flask app to Heroku however upon pushing the code I get the error

2013-06-23T11:23:59.264600+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch 

I'm not sure what to try, I've tried changing the port from 5000 to 33507, but to no avail. My Procfile looks like this:

web: python main.py 

main.py is the main Flask file which initiates the server.

Thanks.

Upvotes: 29

Views: 12712

Answers (4)

msiemens
msiemens

Reputation: 2293

In my Flask app hosted on Heroku, I use this code to start the server:

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

When developing locally, this will use port 5000, in production Heroku will set the PORT environment variable.

(Side note: By default, Flask is only accessible from your own computer, not from any other in the network (see the Quickstart). Setting host='0.0.0.0' will make Flask available from the network)

Upvotes: 52

Gagan Mani
Gagan Mani

Reputation: 161

This also fixes the problem of H20: App boot timeout.

My Procfile looks like this:

web: gunicorn -t 150 -c gunicorn_config.py main:app --bind 0.0.0.0:${PORT}

and main.py:

port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)

Upvotes: 2

Mayur Rawte
Mayur Rawte

Reputation: 191

In addition to msiemens's answer

import os
from run import app as application
if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    application.run(host='0.0.0.0', port=port)

Your Procfile should specify the port address which in this case is stored in the heroku environment variable ${PORT}

web: gunicorn --bind 0.0.0.0:${PORT} wsgi

Upvotes: 9

Miguel Grinberg
Miguel Grinberg

Reputation: 67479

Your main.py script cannot bind to a specific port, it needs to bind to the port number set in the $PORT environment variable. Heroku sets the port it wants in that variable prior to invoking your application.

The error you are getting suggests you are binding to a port that is not the one Heroku expects.

Upvotes: 4

Related Questions