正在尝试对接此flask应用程序...运行以下程序
docker build --tag flask-website .
工程,输出成功构建,成功标记。
编辑:下一个命令起作用
$ 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
好的,然后我运行curl localhost:5000
这就给出了这个错误
curl: (7) Failed to connect to localhost port 5000: Connection refused
好的,足够直截了当,然后我试试这个
docker-compose up
这就是结果
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
然而,尝试导航到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
目录结构如下所示
├──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看起来像这样
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"]
我试着用expos5000不加注释和加注释,没有什么不同
我还更新了目录结构和dockerfile,这消除了我看到的命令行错误
docker-compose看起来像这样
version: "3.7"
services:
web:
image: flask-website:latest
ports:
- 5000:5000
我尝试在app目录之外使用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.
不确定还能尝试什么?我可以用python -m flask run
在本地运行它,但我似乎不能将它停靠,看起来这应该不是很难?
为了完整起见,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看起来像这样
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))
我还在util中尝试了这两种方法,同样的结果因为我是在docker容器中构建的,所以我认为第二个导入是正确的。
我也在app.run上尝试了这个代码块,也得到了相同的结果
if __name__ == "__main__":
app.run()
发布于 2021-10-13 16:21:16
好的,要构建这个镜像并运行容器,需要进行以下更改
dockerfile:
FROM python:3.8
WORKDIR /
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
ENTRYPOINT ["python", "./app.py"]
然后,对主应用程序app.py进行以下更改
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')
然后是util.py,它有一个直到运行docker run [TAG]
才能看到的错误
with open("pickle_mod.pickle", "rb") as f:
__model = pickle.load(f)
然后运行docker build -t [TAG] .
,然后运行docker-compose up
然后导航到localhost监听端口5000,这就是正在运行的容器
发布于 2021-10-05 22:33:07
我在docker中运行了一个简单的flask应用程序,结果如下。在docker编写中,您不需要添加command: python app/app.py
,因为此行是在docker文件中添加的。
Docker编写中唯一需要做的就是端口和镜像名称
https://stackoverflow.com/questions/69457695
复制相似问题