我正在开发一个使用默认模板的简单 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/