我正在尝试通过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

Docker文件
FROM python:3.7

WORKDIR /uhopper
COPY . .

RUN pip install -r requirements.txt

ENTRYPOINT ["python"]
CMD ["uhopper.py"]

这是我的Git Bash:
api - Flask API在Docker上运行但无法访问-LMLPHP

最佳答案

您缺少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

10-04 22:40