将docker flask部署到heroku获取错误R10(引导超时)-> Web进程在启动后60秒内未能绑定到$ PORT

时间:2019-06-28 04:27:07

标签: python-3.x docker heroku flask docker-compose

很抱歉,这个问题与其他许多问题相似。但是我有很多尝试解决这个问题,但没有获得正确的代码来解决。

我使用docker-machine和docker swarm开发flask应用程序。当我在本地运行时,它运行完美。但是当我尝试使用heroku.yml部署到heroku时,出现错误

Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

我刚开始使用heroku,所以我想念一些东西。我希望被疏散的人可以帮助我看看我想念的东西。

.
|-- 2019-06-26-17-05-08.076-VBoxSVC-5799.log
|-- app
|   |-- app.py
|   |-- Dockerfile
|   |-- requirements.txt
|   '-- static
|-- docker-compose.yml
|-- heroku.yml
|-- nginx
|   |-- Dockerfile
|   |-- nginx.conf
|   '-- smartapp.conf
'-- README.md

./ heroku.yml

build:
  docker:
    web: app/Dockerfile
    worker: app/Dockerfile

./ app / Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 8000 available to the world outside this container
#EXPOSE 8000

# Define environment variable
ENV FLASK_APP=smartapp
ENV FLASK_ENV=development

# Run run.py when the container launches
CMD ["gunicorn", "-b", "0.0.0.0", "app:app"]

./ nginx / Dockerfile

FROM nginx
RUN rm /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx/
RUN rm /etc/nginx/conf.d/default.conf
COPY smartapp.conf /etc/nginx/conf.d/

./ app / app.py

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

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')

$ heroku日志--tail

2019-06-28T03:47:57.545950+00:00 heroku[web.1]: Starting process with command `gunicorn -b 0.0.0.0 app:app`
2019-06-28T03:47:59.233102+00:00 app[web.1]: [2019-06-28 03:47:59 +0000] [4] [INFO] Starting gunicorn 19.9.0
2019-06-28T03:47:59.233716+00:00 app[web.1]: [2019-06-28 03:47:59 +0000] [4] [INFO] Listening at: http://0.0.0.0:8000 (4)
2019-06-28T03:47:59.233827+00:00 app[web.1]: [2019-06-28 03:47:59 +0000] [4] [INFO] Using worker: sync
2019-06-28T03:47:59.238431+00:00 app[web.1]: [2019-06-28 03:47:59 +0000] [7] [INFO] Booting worker with pid: 7
2019-06-28T03:48:57.898890+00:00 heroku[web.1]: State changed from starting to crashed
2019-06-28T03:48:57.786102+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-06-28T03:48:57.786193+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-06-28T03:48:57.883116+00:00 heroku[web.1]: Process exited with status 137
2019-06-28T03:48:58.833786+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=quiet-cove-70697.herokuapp.com request_id=a0fd1087-0ac8-44c8-ac31-cb43a46a9449 fwd="103.7.225.121" dyno= connect= service= status=503 bytes= protocol=https
2019-06-28T03:48:59.820240+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=quiet-cove-70697.herokuapp.com request_id=cd9bce77-f818-4516-b2b0-333ab2b95eea fwd="103.7.225.121" dyno= connect= service= status=503 bytes= protocol=https
2019-06-28T03:49:15.792328+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=quiet-cove-70697.herokuapp.com request_id=4ba13aa2-f739-4792-9859-9ad86d9da38b fwd="103.7.225.121" dyno= connect= service= status=503 bytes= protocol=https
2019-06-28T03:49:16.883549+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=quiet-cove-70697.herokuapp.com request_id=1acbfe60-2023-42d5-8091-826d0156d765 fwd="103.7.225.121" dyno= connect= service= status=503 bytes= protocol=https
2019-06-28T03:55:22.425339+00:00 heroku[web.1]: State changed from crashed to starting
2019-06-28T03:55:25.634810+00:00 heroku[web.1]: Starting process with command `gunicorn -b 0.0.0.0 app:app`
2019-06-28T03:55:27.616318+00:00 app[web.1]: [2019-06-28 03:55:27 +0000] [4] [INFO] Starting gunicorn 19.9.0
2019-06-28T03:55:27.616885+00:00 app[web.1]: [2019-06-28 03:55:27 +0000] [4] [INFO] Listening at: http://0.0.0.0:8000 (4)
2019-06-28T03:55:27.616984+00:00 app[web.1]: [2019-06-28 03:55:27 +0000] [4] [INFO] Using worker: sync
2019-06-28T03:55:27.621084+00:00 app[web.1]: [2019-06-28 03:55:27 +0000] [7] [INFO] Booting worker with pid: 7
2019-06-28T03:56:25.725581+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-06-28T03:56:25.725672+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-06-28T03:56:25.821622+00:00 heroku[web.1]: State changed from starting to crashed
2019-06-28T03:56:25.799366+00:00 heroku[web.1]: Process exited with status 137

谢谢

2 个答案:

答案 0 :(得分:1)

我认为您需要将port而不是$PORT传递给您的运行命令:

my_port = '0.0.0.0:' + str(port)
app.run(host=my_port)

您已经为变量port分配了正确的值。

您还需要在EXPOSE的端口Dockerfile

答案 1 :(得分:0)

搜索我的解决方案后,我得到了此链接Adding Gunicorn to your application。 然后像这样更新Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Define environment variable
ENV FLASK_APP=smartapp
ENV FLASK_ENV=development

# Run run.py when the container launches
CMD ["gunicorn", "app:app"]

我从最后一行中删除了此代码(请参见上面的previos代码):

"-b", "0.0.0.0", 

现在我的应用程序可以在heroku中运行了(o.0)