Lui*_*uez 4 python api flask docker
我正在开发一个简单的 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)
Run Code Online (Sandbox Code Playgroud)
我只是调用一个返回并返回“ok”响应的虚拟函数:
def test(features):
return 'ok'
Run Code Online (Sandbox Code Playgroud)
当我使用以下代码直接在我的机器上调用它时:
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 )
Run Code Online (Sandbox Code Playgroud)
它可以正常工作,但如果我从 docker 映像运行它,则会出现以下错误:
ConnectionError: HTTPConnectionPool(host='localhost', port=5000): url 超过最大重试次数:/api/scraping (由 NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f088060ef60>: 无法建立新连接引起:[错误号 111] 连接被拒绝'))
这是我正在使用的 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" ]
Run Code Online (Sandbox Code Playgroud)
我使用相同的 5000 端口:
sudo docker run -d -p 5000:5000 flask-tutorial
Run Code Online (Sandbox Code Playgroud)
你需要expose移植5000你的Dockerfile:
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" ]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5208 次 |
| 最近记录: |