python - Docker 上的 Flask 应用程序 : Max retries exceeded with URL

标签 python api docker flask

我正在开发一个使用默认模板的简单 Flask 应用程序:

from flask import render_template
import connexion

# Create the application instance
app = connexion.App(__name__, specification_dir="./")

# read the swagger.yml file to configure the endpoints
app.add_api("swagger.yml")


# Create a URL route in our application for "/"
@app.route("/")
def home():
    """
    This function just responds to the browser URL
    localhost:5000/

    :return:        the rendered template "home.html"
    """
    return render_template("home.html")


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

我只是调用了一个返回“ok”响应的虚拟函数:

def test(features):
    return 'ok'

当我使用以下代码在我的机器上直接调用它时:

headers = {'content-type': 'application/json'}
#url = "http://localhost:5000/api/document"

url = "http://localhost:5000/api/scraping"
data = {'features':features}

response = requests.post(url,data=json.dumps(data), headers=headers )

它没有任何问题,但是如果我从 docker 镜像运行它,我会收到以下错误:

ConnectionError: HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url: /api/scraping (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f088060ef60>: Failed to establish a new connection: [Errno 111] Connection refused'))

这是我正在使用的 docker 文件:

FROM ubuntu:18.04 



RUN apt-get update -y && \
    apt-get install -y python-pip python-dev

# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app

ENTRYPOINT [ "python" ]

CMD [ "app.py" ]

我正在使用相同的 5000 端口:

sudo docker run -d -p 5000:5000 flask-tutorial

最佳答案

您需要在 Dockerfile公开端口5000:

FROM ubuntu:18.04 



RUN apt-get update -y && \
    apt-get install -y python-pip python-dev

# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app
EXPOSE 5000

ENTRYPOINT [ "python" ]

CMD [ "app.py" ]

关于python - Docker 上的 Flask 应用程序 : Max retries exceeded with URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57519290/

相关文章:

python - 从 fixture 内部跳过测试

python - 如何使用 NumPy 生成具有条件概率的 x 和 y 样本

java - 在 Docker 容器中部署 WAR 文件

python - 有没有办法动态地将默认数据类型(CSV)更改为MySQL数据类型

node.js - 添加服务的自定义端点 ( Feathersjs )

javascript - 使用 Node Express 应用程序在 Zabbix api 上提取数据

api - bookmyshow如何解密Whatsapp api发送文本?

image - 使 Docker 镜像相互通信

docker - 错误配置无效,在Kafka集群中异常退出

python - 按月、年对日期列表进行分组