【发布时间】:2019-11-17 20:20:04
【问题描述】:
我正在尝试使用 Docker 运行我的 API。我已经设法构建并运行它,但是当我在 Postman 中对其进行测试时,它不起作用。
我有一个文件夹,其中包含 uhopper.py(即 API)、requirements.txt 和 Dockerfile。
一切似乎都正常,但是当我在“http://127.0.0.1:5000/profile/john”上发出 GET 请求时,它没有给出响应并发生错误(“连接到 http://127.0.0.1:5000/profile/john 时出错。”)
我希望有人可以帮助我,在此先感谢!
uhopper.py
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
john = {
"http://en.wikipedia.org/wiki/Category:Writers_from_Belfast": 1,
"http://en.wikipedia.org/wiki/Category:People_educated_at_Newstead_Wood_School": 2,
}
mac = {
"http://en.wikipedia.org/wiki/Category:1998_establishments_in_New_York": 1,
"http://en.wikipedia.org/wiki/Category:Public_Interest_Research_Groups": 1,
}
class Profile(Resource):
def get(self, name):
if name == 'john':
return john, 200
elif name == 'mac':
return mac, 200
return "Not found", 400
api.add_resource(Profile, "/profile/<string:name>")
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
requirements.txt
Flask>=1.1.1
Flask-RESTful>=0.3.7
Dockerfile
FROM python:3.7
WORKDIR /uhopper
COPY . .
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["uhopper.py"]
【问题讨论】: