[英]Docker run error - Failed to find Flask application or factory in module "app". Use "FLASK_APP=app:name to specify one
trying to dockerize this flask app... running the following尝试 dockerize 这个烧瓶应用程序......运行以下
docker build --tag flask-website .
works, output successfully built, successfully tagged.工作,输出成功构建,成功标记。
edit: the next command works编辑:下一个命令有效
$ docker run --publish 5000:5000 flask-website
* 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
ok, so then I run curl localhost:5000
好的,那么我运行
curl localhost:5000
which gives this error这给出了这个错误
curl: (7) Failed to connect to localhost port 5000: Connection refused
ok straight forward enough, so then I try this好吧,直截了当,所以我试试这个
docker-compose up
and this results结果
Creating network "app_default" with the default driver
Creating app_web_1 ... done
Attaching to app_web_1
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production WSGI server instead.
web_1 | * Debug mode: off
however trying to navigate to localhost:5000 I get但是尝试导航到 localhost:5000 我得到
This site can’t be reachedThe webpage at http://localhost:5000/
might be temporarily down or it may have moved permanently
to a new web address.
ERR_SOCKET_NOT_CONNECTED
directory structure looks like this目录结构看起来像这样
├──app_folder/
└── app/
| ├── static/
| | └── css/
| | └──app.css
| | └── js/
| | └──app.js
| └── templates/
| | └── app.html
| | └── subapp.html
| | └── subapp1.html
| | └── subapp2.html
| | └── subapp3.html
| └── app.py
| └── util.py
| └── pickle_file.pickle
| └── requirements.txt
| └── Dockerfile
| └── Makefile
| └── docker-compose.yml
dockerfile looks like this dockerfile 看起来像这样
FROM python:3.8
ENV PYTHONUNBUFFERED=1
WORKDIR /
COPY requirements.txt requirements.txt
COPY . .
RUN pip install -r requirements.txt
# EXPOSE 5000
CMD [ "python", "-m" , "flask", "run", "--host=0.0.0.0"]
I had tried with EXPOSE 5000 uncommented and commented, making no difference我曾尝试使用 EXPOSE 5000 取消注释和评论,没有任何区别
I also updated the directory structure and dockerfile, which got rid of the command line error I was seeing我还更新了目录结构和 dockerfile,这消除了我看到的命令行错误
docker-compose looks like this docker-compose 看起来像这样
version: "3.7"
services:
web:
image: flask-website:latest
ports:
- 5000:5000
I tried with the dockerfile, docker-compose, makefile, and requirements outside of the app directory and a slightly modified dockerfile on the WORKDIR line, that resulted in this error我尝试在应用程序目录之外使用 dockerfile、docker-compose、makefile 和需求,并在 WORKDIR 行上使用稍微修改的 dockerfile,这导致了此错误
Error: Failed to find Flask application or factory in module "app". Use "FLASK_APP=app:name to specify one.
not sure what else to try?不知道还有什么可以尝试的? I can run it locally with
python -m flask run
, but I cannot seem to dockerize it, seems like this should not be this difficult?我可以使用
python -m flask run
在本地运行它,但我似乎无法对其进行 dockerize,这似乎不应该这么困难?
for completeness sake, app.py looks like this为了完整起见,app.py 看起来像这样
from flask import Flask, request, jsonify
from flask import render_template, redirect
import json
import util
app = Flask(__name__, template_folder="templates", static_folder="static")
@app.route("/", methods=["GET", "POST"])
def index():
return render_template("app.html")
@app.route("/predict_home_price", methods=["GET", "POST"])
def make_prediction():
x = request.form["x"]
y = float(request.form["y"]
response = jsonify(
{
"prediction": util.prediction(x, y)
}
)
response.headers.add("Access-Control-Allow-Origin", "*")
return response
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=5000)
util.py looks like this util.py 看起来像这样
import pickle
import pandas as pd
from scipy.special import inv_boxcox
# to run locally uncomment out the following
# with open("/path/to/pickle/app/pickle_mod.pickle", "rb") as f:
# to run in docker use the following
with open("app/pickle_mod.pickle", "rb") as f:
__model = pickle.load(f)
def prediction(x, y):
lambs = 0.205
a = [[x, y]]
cols = ["x", "y"]
my_data = pd.DataFrame(data=a, columns=cols)
pred = inv_boxcox(__model.predict(my_data)[0], lambs)
return f"${round(pred)}"
if __name__ == "__main__":
print(prediction(5, 4))
I have also tried both ways with the pickle import in util, same result - I thought because I was building it in a docker container that the second import was correct我也尝试过使用 util 中的 pickle 导入的两种方法,结果相同 - 我认为因为我是在 docker 容器中构建它,所以第二次导入是正确的
I have also tried this block for app.run as well with the same result我也为 app.run 尝试了这个块,结果相同
if __name__ == "__main__":
app.run()
I have ran a simple flask app in docker and here is the result.我在 docker 中运行了一个简单的烧瓶应用程序,结果如下。 In your docker compose, you do not need to add the
command: python app/app.py
as this line was added in the Dockerfile.在您的 docker compose 中,您不需要添加
command: python app/app.py
因为这一行已添加到 Dockerfile 中。
The only thing you need in your Docker Compose is ports and image name您在 Docker Compose 中唯一需要的是端口和映像名称
ok the following changes were required to get this image to build and the container to run好的,需要进行以下更改才能构建此映像并运行容器
dockerfile:码头档案:
FROM python:3.8
WORKDIR /
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
ENTRYPOINT ["python", "./app.py"]
then the following change to the main application app.py然后对主应用程序 app.py 进行以下更改
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')
then to util.py, which had an error I did not see until running docker run [TAG]
然后到 util.py,它有一个错误,我直到运行
docker run [TAG]
才看到
with open("pickle_mod.pickle", "rb") as f:
__model = pickle.load(f)
then run docker build -t [TAG] .
然后运行
docker build -t [TAG] .
then run docker-compose up
然后运行
docker-compose up
then navigate to localhost listening on port 5000 and there is the running container然后导航到 localhost 监听端口 5000 并且有正在运行的容器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.