【问题标题】:Docker flask app not working - port issuesDocker 烧瓶应用程序不工作 - 端口问题
【发布时间】:2020-02-28 13:05:42
【问题描述】:

我正在尝试通过 docker 运行烧瓶。

我的码头文件:

FROM python:3

# set a directory for the app
WORKDIR /usr/src/app

# copy all the files to the container
COPY . .

# install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# tell the port number the container should expose
EXPOSE 3000

# run the command
ENTRYPOINT ["python", "app.py"]

我的 app.py

import os

from flask import Flask, request, jsonify

from submission import SubmissionResult

app = Flask(__name__)



@app.route("/health")
def health_check():
    return "healthy"

if __name__ == "__main__":
    app.run(debug=True)

我跑了docker run <my_image>

当我在邮递员上点击“/health”时,我收到一条错误消息:

Could not get any response
There was an error connecting to .
Why this might have happened:
The server couldn't send a response:
Ensure that the backend is working properly
Self-signed SSL certificates are being blocked:
Fix this by turning off 'SSL certificate verification' in Settings > General
Proxy configured incorrectly
Ensure that proxy is configured correctly in Settings > Proxy
Request timeout:
Change request timeout in Settings > General

这很奇怪,因为我的烧瓶应用程序没有显示任何调试日志,所以大概请求没有到达服务器。我没有正确暴露端口吗?

我该如何解决这个问题?

【问题讨论】:

    标签: python docker flask


    【解决方案1】:

    您没有在 Python 代码上指定输出端口。它映射到默认端口,即 5000。如果要暴露到端口 3000,请使用 app.run(debug=True, port=3000)。您还必须在主机上导出 Docker 端口,在 <my_image> 之前调用 -p 3000:3000。它会告诉 Docker 将镜像的 3000 端口映射到主机的 3000 端口。

    【讨论】:

    • hm 很有趣,这很有效。我不太明白 Dockerfile 中 EXPOSE 的目的
    • EXPOSE 仅用于文档。将它放在 Dockerfile 中是一个很好的做法,因为它告诉正在使用 Dockerfile 的人正在使用的端口。
    猜你喜欢
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2021-08-31
    • 2015-04-18
    • 2021-05-18
    • 2016-03-09
    • 2017-10-18
    相关资源
    最近更新 更多