Issue
I am trying to deploy a dockerized flask app into Heroku.
I am constantly getting "Port in use by another program" everytime I deploy. I have no idea how to tackle this problem. Please could someone help?
DOCKERFILE
FROM python:3.8-slim-buster
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Pacific/Auckland
RUN echo $TZ > /etc/timezone && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
WORKDIR /app
COPY ./requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
ENTRYPOINT ["python3"]
CMD ["myapp.py"]
myapp.py
import os
import requests
import json
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/')
def parse_data():
bla bla
return result
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port, debug=True)
Procfile
web: gunicorn myapp:app
LOGS FROM HEROKU
2022-06-04T11:33:05.715431+00:00 app[web.1]: [2022-06-04 11:33:05 +0000] [1560] [INFO] Booting worker with pid: 1560
2022-06-04T11:33:05.721157+00:00 app[web.1]: * Serving Flask app 'myapp' (lazy loading)
2022-06-04T11:33:05.721175+00:00 app[web.1]: * Environment: production
2022-06-04T11:33:05.721202+00:00 app[web.1]: WARNING: This is a development server. Do not use it in a production deployment.
2022-06-04T11:33:05.721219+00:00 app[web.1]: Use a production WSGI server instead.
2022-06-04T11:33:05.721232+00:00 app[web.1]: * Debug mode: on
2022-06-04T11:33:05.724607+00:00 app[web.1]: Address already in use
2022-06-04T11:33:05.724627+00:00 app[web.1]: Port 4050 is in use by another program. Either identify and stop that program, or start the server with a different port.
2022-06-04T11:33:05.724800+00:00 app[web.1]: [2022-06-04 11:33:05 +0000] [1559] [INFO] Worker exiting (pid: 1559)
The web page is rendered well from
Solution
Try removing this line from the app:
app.run(host='0.0.0.0', port=port, debug=True)
or place it in the usual if
block, which ensures it only runs the development server when launched with the python
executable instead of gunicorn
:
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port, debug=True)
The way you have this positioned just now, it looks like heroku is launching the server with gunicorn
(correctly) but then the dev server launches with app.run
afterwards, which causes the 'port in use error'.
The tell-tale sign is that you see the usual dev server warning in the logs, despite the Procfile
(again correctly) launching the app with gunicorn
.
Answered By - v25
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.