【问题标题】:Dockerized Flask App / Crashing on Heroku while locally running image works fineDockerized Flask App / 在本地运行图像时在 Heroku 上崩溃工作正常
【发布时间】:2021-08-15 15:07:29
【问题描述】:

在本地构建和运行 docker 映像时,flask 应用程序成功启动,一切正常。

但是,当部署到 Heroku 时,应用会崩溃并立即存在。

Dockerfile

FROM python:3.9.4

ADD . /blockchain-py

WORKDIR /blockchain-py

RUN pip3 install -r requirements.txt

EXPOSE 5000

ENTRYPOINT [ "python3" ]

CMD ["server.py"]

heroku.yml

build:
  docker:
    web: Dockerfile

我也尝试过添加 Procfile(无济于事)

web: gunicorn --bind 0.0.0.0:${PORT} wsgi server:app --log-file=-

heroku 日志中的错误消息:

2021-05-27T08:01:39.206659+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-27T08:01:55.599292+00:00 heroku[web.1]: Starting process with command `python3`
2021-05-27T08:01:58.310259+00:00 heroku[web.1]: Process exited with status 0
2021-05-27T08:01:58.390600+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-27T08:01:58.409515+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-27T08:02:18.130426+00:00 heroku[web.1]: Starting process with command `python3`
2021-05-27T08:02:21.371471+00:00 heroku[web.1]: Process exited with status 0
2021-05-27T08:02:21.466571+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-27T08:02:23.139469+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=blockchain-server-py.herokuapp.com request_id=69be5144-0665-4bc6-8616-e15b34ea370f fwd="95.91.235.135" dyno= connect= service= status=503 bytes= protocol=https
2021-05-27T08:02:23.845517+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=blockchain-server-py.herokuapp.com request_id=7262f8a8-264a-47cb-9cfa-f67d27ee4470 fwd="95.91.235.135" dyno= connect= service= status=503 bytes= protocol=https

server.py

import os
from typing import Dict, Union
from server.response import Response
from blockchain import *
from flask import Flask, request, send_from_directory
from flask_cors import CORS
from server.responseHelpers import get_message, get_missing_fields, get_serializable_block, get_serializable_transaction, has_all_required_fields, jsonify_chain
from server.models.statusCodes import HttpStatusCodes
from server.requestHelpers import get_param
from core.blockchainFactory import BlockchainFileStorageFactory

app = Flask(__name__)
CORS(app)

blockchain = Blockchain(BlockchainFileStorageFactory)

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


@app.route('/', methods=[HttpStatusCodes.GET])
def get_ui():
    return send_from_directory('ui/templates', 'node.html')


# ... some more routes 

print('Starting Server...')
if __name__ == '__main__':
    app.run(host, port, debug=True)

【问题讨论】:

  • 你能分享你的server.py文件数据吗?
  • 添加 server.py 文件(缩短,不包括所有路由定义)
  • 不应该 methods=[HttpStatusCodes.GET]methods=[GET] 吗?
  • 不,它工作正常,我创建了自己的状态码包装器。 class HttpStatusCodes(str, Enum): GET = 'GET' POST = 'POST' PUT = 'PUT'
  • 检查如何在heroku服务器中调用python,即用pythonpython3调用

标签: python docker flask heroku


【解决方案1】:

访问环境变量时使用大写字母 (PORT)

port = int(os.environ.get("PORT", 5000))

【讨论】:

  • 谢谢,好点子。但不能解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 2021-04-08
  • 2021-08-25
  • 1970-01-01
  • 2020-03-16
相关资源
最近更新 更多