我正在尝试通过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"]
答案 0 :(得分:0)
您缺少Flask导入:
from flask import Flask
您可以对Dockerfile重新排序以保存复制路径:
FROM python:3.7
WORKDIR /uhopper
COPY . .
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["uhopper.py"]
我认为使用绝对路径CMD ["/uhopper/uhopper.py"]
我建议您也指定(至少要至少)导入的版本:
Flask>=1.1.1
Flask-RESTful>=0.3.7