在 Heroku Web 进程上使用 Dockerfile 的 Flask App 在启动后 60 秒内无法绑定到 $PORT
Flask App with Dockerfile on Heroku Web process failed to bind to $PORT within 60 seconds of launch
当我使用 Docker 部署到 Heroku 时,我的烧瓶应用程序出现错误并显示错误消息:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
这是我的Docker文件:
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
EXPOSE 5000
CMD [ "gunicorn", "--config", "gunicorn_config.py", "run:app"]
这里是gunicorn_config.py的内容:
bind = "0.0.0.0:5000"
workers = 4
threads = 4
timeout = 120
这是我在 gitlab 中使用的脚本命令-ci:
script:
- docker pull $CI_REGISTRY_IMAGE/$IMAGE_NAME:latest
- docker tag $CI_REGISTRY_IMAGE/$IMAGE_NAME:latest registry.heroku.com/my-app/web
- docker login --username=_ --password=$HEROKU_API_KEY registry.heroku.com
- docker push registry.heroku.com/my-app/web
- docker run --rm -e HEROKU_API_KEY=$HEROKU_API_KEY wingrunr21/alpine-heroku-cli container:release web --app my-app
- echo "App deployed to server at https://my-app.herokuapp.com/"
至此一切都成功了。我可以部署到 Heroku。
但是当我尝试打开应用程序时它崩溃了。使用此命令,我看到了日志 - Heroku logs --tail
Stopping all processes with SIGTERM
2022-04-01T06:23:03.125358+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [9] [INFO] Worker exiting (pid: 9)
2022-04-01T06:23:03.125467+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [10] [INFO] Worker exiting (pid: 10)
2022-04-01T06:23:03.125524+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [4] [INFO] Handling signal: term
2022-04-01T06:23:03.131769+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [4] [WARNING] Worker with pid 9 was terminated due to signal 15
2022-04-01T06:23:03.326507+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [4] [INFO] Shutting down: Master
2022-04-01T06:23:03.518148+00:00 heroku[web.1]: Process exited with status 0
2022-04-01T06:23:06.918424+00:00 heroku[web.1]: Starting process with command `gunicorn --config gunicorn_config.py run:app`
2022-04-01T06:23:07.717472+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [4] [INFO] Starting gunicorn 20.1.0
2022-04-01T06:23:07.717835+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [4] [INFO] Listening at: http://0.0.0.0:5000 (4)
2022-04-01T06:23:07.717884+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [4] [INFO] Using worker: gthread
2022-04-01T06:23:07.720605+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [6] [INFO] Booting worker with pid: 6
2022-04-01T06:23:07.727693+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [7] [INFO] Booting worker with pid: 7
2022-04-01T06:23:07.776540+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [8] [INFO] Booting worker with pid: 8
2022-04-01T06:23:07.848862+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [9] [INFO] Booting worker with pid: 9
2022-04-01T06:24:07.069895+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-04-01T06:24:07.138987+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-04-01T06:24:07.316175+00:00 heroku[web.1]: Process exited with status 137
2022-04-01T06:24:07.373029+00:00 heroku[web.1]: State changed from starting to crashed
2022-04-01T06:24:07.445856+00:00 heroku[web.1]: State changed from crashed to starting
2022-04-01T06:24:13.147942+00:00 heroku[web.1]: Starting process with command `gunicorn --config gunicorn_config.py run:app`
2022-04-01T06:24:14.202704+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [4] [INFO] Starting gunicorn 20.1.0
2022-04-01T06:24:14.203085+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [4] [INFO] Listening at: http://0.0.0.0:5000 (4)
2022-04-01T06:24:14.203129+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [4] [INFO] Using worker: gthread
2022-04-01T06:24:14.206105+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [6] [INFO] Booting worker with pid: 6
2022-04-01T06:24:14.239507+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [7] [INFO] Booting worker with pid: 7
2022-04-01T06:24:14.327054+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [8] [INFO] Booting worker with pid: 8
2022-04-01T06:24:14.399461+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [9] [INFO] Booting worker with pid: 9
2022-04-01T06:25:13.653641+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-04-01T06:25:13.690271+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-04-01T06:25:13.868500+00:00 heroku[web.1]: Process exited with status 137
2022-04-01T06:25:13.990309+00:00 heroku[web.1]: State changed from starting to crashed
似乎问题与端口有关,但我在 gunicorn_config.py 文件中将其设置为 5000。它甚至在日志中打印它正在监听端口 5000 Listening在:http://0.0.0.0:5000 (4).
也许有人能看出问题出在哪里?
尝试以下 gunicorn 配置:
bind = "0.0.0.0:" + os.getenv("PORT")
workers = 4
threads = 4
timeout = 120
当我使用 Docker 部署到 Heroku 时,我的烧瓶应用程序出现错误并显示错误消息:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
这是我的Docker文件:
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
EXPOSE 5000
CMD [ "gunicorn", "--config", "gunicorn_config.py", "run:app"]
这里是gunicorn_config.py的内容:
bind = "0.0.0.0:5000"
workers = 4
threads = 4
timeout = 120
这是我在 gitlab 中使用的脚本命令-ci:
script:
- docker pull $CI_REGISTRY_IMAGE/$IMAGE_NAME:latest
- docker tag $CI_REGISTRY_IMAGE/$IMAGE_NAME:latest registry.heroku.com/my-app/web
- docker login --username=_ --password=$HEROKU_API_KEY registry.heroku.com
- docker push registry.heroku.com/my-app/web
- docker run --rm -e HEROKU_API_KEY=$HEROKU_API_KEY wingrunr21/alpine-heroku-cli container:release web --app my-app
- echo "App deployed to server at https://my-app.herokuapp.com/"
至此一切都成功了。我可以部署到 Heroku。
但是当我尝试打开应用程序时它崩溃了。使用此命令,我看到了日志 - Heroku logs --tail
Stopping all processes with SIGTERM
2022-04-01T06:23:03.125358+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [9] [INFO] Worker exiting (pid: 9)
2022-04-01T06:23:03.125467+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [10] [INFO] Worker exiting (pid: 10)
2022-04-01T06:23:03.125524+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [4] [INFO] Handling signal: term
2022-04-01T06:23:03.131769+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [4] [WARNING] Worker with pid 9 was terminated due to signal 15
2022-04-01T06:23:03.326507+00:00 app[web.1]: [2022-04-01 06:23:03 +0000] [4] [INFO] Shutting down: Master
2022-04-01T06:23:03.518148+00:00 heroku[web.1]: Process exited with status 0
2022-04-01T06:23:06.918424+00:00 heroku[web.1]: Starting process with command `gunicorn --config gunicorn_config.py run:app`
2022-04-01T06:23:07.717472+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [4] [INFO] Starting gunicorn 20.1.0
2022-04-01T06:23:07.717835+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [4] [INFO] Listening at: http://0.0.0.0:5000 (4)
2022-04-01T06:23:07.717884+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [4] [INFO] Using worker: gthread
2022-04-01T06:23:07.720605+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [6] [INFO] Booting worker with pid: 6
2022-04-01T06:23:07.727693+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [7] [INFO] Booting worker with pid: 7
2022-04-01T06:23:07.776540+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [8] [INFO] Booting worker with pid: 8
2022-04-01T06:23:07.848862+00:00 app[web.1]: [2022-04-01 06:23:07 +0000] [9] [INFO] Booting worker with pid: 9
2022-04-01T06:24:07.069895+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-04-01T06:24:07.138987+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-04-01T06:24:07.316175+00:00 heroku[web.1]: Process exited with status 137
2022-04-01T06:24:07.373029+00:00 heroku[web.1]: State changed from starting to crashed
2022-04-01T06:24:07.445856+00:00 heroku[web.1]: State changed from crashed to starting
2022-04-01T06:24:13.147942+00:00 heroku[web.1]: Starting process with command `gunicorn --config gunicorn_config.py run:app`
2022-04-01T06:24:14.202704+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [4] [INFO] Starting gunicorn 20.1.0
2022-04-01T06:24:14.203085+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [4] [INFO] Listening at: http://0.0.0.0:5000 (4)
2022-04-01T06:24:14.203129+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [4] [INFO] Using worker: gthread
2022-04-01T06:24:14.206105+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [6] [INFO] Booting worker with pid: 6
2022-04-01T06:24:14.239507+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [7] [INFO] Booting worker with pid: 7
2022-04-01T06:24:14.327054+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [8] [INFO] Booting worker with pid: 8
2022-04-01T06:24:14.399461+00:00 app[web.1]: [2022-04-01 06:24:14 +0000] [9] [INFO] Booting worker with pid: 9
2022-04-01T06:25:13.653641+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-04-01T06:25:13.690271+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-04-01T06:25:13.868500+00:00 heroku[web.1]: Process exited with status 137
2022-04-01T06:25:13.990309+00:00 heroku[web.1]: State changed from starting to crashed
似乎问题与端口有关,但我在 gunicorn_config.py 文件中将其设置为 5000。它甚至在日志中打印它正在监听端口 5000 Listening在:http://0.0.0.0:5000 (4).
也许有人能看出问题出在哪里?
尝试以下 gunicorn 配置:
bind = "0.0.0.0:" + os.getenv("PORT")
workers = 4
threads = 4
timeout = 120