[英]Dockerize Flask: Error: While importing 'app', an ImportError was raised
I am trying to dockerize my Flask API.我正在尝试对接我的 Flask API。 As soon as I try to start my image I receive the message:
一旦我尝试启动我的图像,我就会收到以下消息:
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.
Error: While importing 'app', an ImportError was raised.
If I am starting the Flask App with my terminal python -m flask run
everything works like intended.如果我使用终端
python -m flask run
启动 Flask 应用程序,一切都会按预期工作。 And right now I am stuck on this problem.现在我被困在这个问题上。
Here is my Code:这是我的代码:
from flask import Flask
from bson import json_util
from flask_pymongo import PyMongo
from flask_cors import CORS
import json
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://194.163.147.192:27017/test"
CORS(app)
mongo = PyMongo(app)
def parse_json(data):
return json.loads(json_util.dumps(data))
@app.route('/')
def home():
return 'Hello'
@app.route('/residential', methods=['GET'])
def find_residential(): # put application's code here
test = mongo.db.acs.find_one({"name": "Residential"})
response = Flask.jsonify(parse_json(test))
response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route('/commercial', methods=['GET'])
def find_commercial(): # put application's code here
test = mongo.db.acs.find_one({"name": "Commercial"})
response = Flask.jsonify(parse_json(test))
response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route('/healthcare', methods=['GET'])
def find_health_care(): # put application's code here
test = mongo.db.acs.find_one({"name": "Health Care"})
response = Flask.jsonify(parse_json(test))
response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route('/germany', methods=['GET'])
def find_germany():
test = mongo.db.germanies.find_one()
response = Flask.jsonify(parse_json(test))
response.headers.add('Access-Control-Allow-Origin', '*')
return response
if __name__ == '__main__':
app.debug = False
app.run()
My requirements.txt looks like this我的 requirements.txt 看起来像这样
bson==0.5.10
click==8.0.3
colorama==0.4.4
Flask==2.0.2
Flask-Cors==3.0.10
Flask-PyMongo==2.3.0
itsdangerous==2.0.1
Jinja2==3.0.2
MarkupSafe==2.0.1
pymongo==3.12.1
python-dateutil==2.8.2
six==1.16.0
Werkzeug==2.0.2
My Dockerfile looks like this我的 Dockerfile 看起来像这样
FROM python:3.8-slim-buster
WORKDIR /api
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
I am thankful for any help :)我很感谢任何帮助:)
Project structure is like:项目结构如下:
API
L venv
L app.py
L Dockerfile
L requirements.txt
You need to set the FLASK_APP
environment variable.您需要设置
FLASK_APP
环境变量。 See https://flask.palletsprojects.com/en/2.0.x/cli/见https://flask.palletsprojects.com/en/2.0.x/cli/
Your issue is, I think, with your requirements file.我认为您的问题与您的需求文件有关。 In that you include
bson
as a dependency, which is also included in the pymongo
library.因为您将
bson
作为依赖项包含在内,该依赖项也包含在pymongo
库中。 See this question .看到这个问题。 Removing it seems to solve the issue:
删除它似乎可以解决问题:
~/tmp/so_q $ docker build -t myimage . 8s nathanielford@nford 20:51:04
Sending build context to Docker daemon 5.12kB
...
Successfully tagged myimage:latest
~/tmp/so_q $ docker run myimage 13s nathanielford@nford 20:51:26
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://192.168.9.2:5000/ (Press CTRL+C to quit)
Were you able to solve this problem?你能解决这个问题吗? I'm having the same issue and it's maddening.
我有同样的问题,这很令人抓狂。
Not sure if it will help... but I had the same issue too and I've just fixed it changing my entrypoint.sh
file.不确定它是否会有所帮助......但我也遇到了同样的问题,我刚刚修复了它,更改了我的
entrypoint.sh
文件。
Before I was starting the flask app using:在我使用以下方法启动烧瓶应用程序之前:
flask run --host 0.0.0.0 --port 5000
And now I'm using gunicorn:现在我正在使用 gunicorn:
gunicorn -b 0.0.0.0:5000 app:app
So I had to add gunicorn
in the requirements.txt
file too.所以我也不得不在
requirements.txt
文件中添加gunicorn
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.