[Solved] Heroku Error: Web process failed to bind to $PORT within 60 seconds of launch

Error description

If the set port number is less than 1000, an error will be reported, and there is no permission

2022-03-29T10:23:16.651636+00:00 heroku[web.1]: State changed from crashed to starting
2022-03-29T10:23:28.994173+00:00 heroku[web.1]: Starting process with command `python /code/server3.py`
2022-03-29T10:23:30.091143+00:00 app[web.1]: Traceback (most recent call last):
2022-03-29T10:23:30.091204+00:00 app[web.1]: File "/code/server3.py", line 247, in <module>
2022-03-29T10:23:30.091205+00:00 app[web.1]: serverSocket.bind(("127.0.0.1", serverPort))
2022-03-29T10:23:30.091208+00:00 app[web.1]: PermissionError: [Errno 13] Permission denied
2022-03-29T10:23:30.234919+00:00 heroku[web.1]: Process exited with status 1
2022-03-29T10:23:30.279569+00:00 heroku[web.1]: State changed from starting to crashed

If you set the port number to 8080, you will not be connected within 60 seconds

2022-03-29T10:53:34.400924+00:00 heroku[web.1]: State changed from crashed to starting
2022-03-29T10:53:49.432473+00:00 heroku[web.1]: Starting process with command `python /code/server3.py`
2022-03-29T10:53:33.432308+00:00 app[api]: Release v9 created by user ***@icloud.com
2022-03-29T10:53:33.432308+00:00 app[api]: Deployed web (350d1bd5740a) by user ***@icloud.com
2022-03-29T10:54:49.500818+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-03-29T10:54:49.587777+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-03-29T10:54:49.800462+00:00 heroku[web.1]: Process exited with status 137

If the port number in the environment variable is referenced dynamically, the acquisition fails

2022-03-29T11:12:03.984493+00:00 heroku[web.1]: State changed from crashed to starting
2022-03-29T11:12:19.984320+00:00 heroku[web.1]: Starting process with command `python /code/server3.py`
2022-03-29T11:12:21.706887+00:00 heroku[web.1]: Process exited with status 1
2022-03-29T11:12:21.536737+00:00 app[web.1]: Traceback (most recent call last):
2022-03-29T11:12:21.536756+00:00 app[web.1]: File "/code/server3.py", line 248, in <module>
2022-03-29T11:12:21.536757+00:00 app[web.1]: serverSocket.bind(("0.0.0.0", serverPort))
2022-03-29T11:12:21.536757+00:00 app[web.1]: TypeError: an integer is required (got type NoneType)
2022-03-29T11:12:21.769096+00:00 heroku[web.1]: State changed from starting to crashed

No matter the local address is not written,
ServerSocket Bind ((“”, serverport))
or 127.0.0.1
ServerSocket Bind ((“127.0.0.1”, serverport))
or write 0.0.0
ServerSocket Bind ((“0.0.0.0”, serverport))
doesn’t work

 

Solution (Python Project):

Dynamic binding port number is required

To write this in dockerfile, pass in $post

#Base image based on
FROM python:3.4

#code added to code folder
ADD . /pythonProject /code

# Set the code folder to be the working directory
WORKDIR /code

# Install support
#RUN pip install -r requirements.txt

CMD python /code/server3.py $PORT

Python file writing format,  obtain through parameters in server.py

serverPort = int(sys.argv[1])

The complete structure is as follows

if __name__ == '__main__':
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverPort = int(sys.argv[1])
    serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    serverSocket.bind(("", serverPort))

    serverSocket.listen(5)
    print('The server is running')
    # Main web server loop. It simply accepts TCP connections, and get the request processed in seperate threads.
    while True:
        # Set up a new connection from the client
        connectionSocket, addr = serverSocket.accept()
        # Clients timeout after 60 seconds of inactivity and must reconnect.
        connectionSocket.settimeout(600)
        # start new thread to handle incoming request
        _thread.start_new_thread(process, (connectionSocket,))

Solution (Nodejs Project):

// production
config.port = process.env.PORT

app.listen(config.port, () => {
  logger.info('Listening on port %d', config.port);
});

or

.listen(process.env.PORT || 5000)

or

production: {
    server: {
        host: '0.0.0.0',
        port: process.env.PORT
    }
}

Read More: