【发布时间】:2020-07-03 00:02:25
【问题描述】:
我正在尝试构建一个 docker 来运行烧瓶应用程序。我以前从来没有这样做过。我有烧瓶应用程序在本地工作。这是我的方法:
我的项目目录结构如下:
model.pkl README.md images/ static/
Dockerfile flaskapp.py requirements.txt templates/
我可以通过运行 python flaskapp.py 来启动烧瓶应用程序,它会在我的浏览器中(本地)运行。
我想创建一个 Docker,这样其他机器就可以运行这个项目,而无需处理所有依赖项。为此,我做了以下工作:
- 我在里面创建了一个 Dockerfile:
FROM python:3
COPY requirements.txt /tmp
COPY flaskapp.py /tmp
COPY model.pkl /tmp
COPY images /tmp
COPY static /tmp
COPY templates /tmp
WORKDIR /tmp
ADD flaskapp.py /
RUN pip install -r requirements.txt
CMD [ "python", "flaskapp.py" ]
-
运行命令
docker build -t python-barcode . -
那行得通,所以。我跑了
docker run python-barcode。终端打印出* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit),但是没用,浏览器出现这个错误:
This site can’t be reached0.0.0.0 refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
所以我做了一些挖掘,并将我的 Dockerfile 更新为这个(添加最后一行):
FROM python:3
COPY requirements.txt /tmp
COPY flaskapp.py /tmp
COPY model.pkl /tmp
COPY images /tmp
COPY static /tmp
COPY templates /tmp
WORKDIR /tmp
ADD flaskapp.py /
RUN pip install -r requirements.txt
CMD [ "python", "flaskapp.py" ]
CMD ["flask", "run", "--host", "0.0.0.0" ]
- 然后再次运行
docker run python-barcode,我得到这个错误:
Usage: flask run [OPTIONS]
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
For more information see http://flask.pocoo.org/docs/latest/quickstart/
我应该如何进行?
如果相关,我的flaskapp.py 看起来像这样:
model = load_learner('', 'model.pkl')
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
def classify(document):
X = document
y = model.predict(X)
return y
class ReviewForm(Form):
pred = TextAreaField('',[validators.DataRequired(),validators.length(min=1)])
@app.route('/')
def index():
form = ReviewForm(request.form)
return render_template('reviewform.html', form=form)
@app.route('/results', methods=['POST'])
def results():
form = ReviewForm(request.form)
if request.method == 'POST' and form.validate():
sequence = request.form['pred']
y = classify(sequence)
return render_template('results.html',
y = y)
return render_template('reviewform.html', form=form)
if __name__ == '__main__':
app.run(host= '0.0.0.0')
编辑 1
现在我收到此错误:
[2020-07-03 00:29:51,222] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "flaskapp.py", line 94, in index
return render_template('reviewform.html', form=form)
File "/usr/local/lib/python3.8/site-packages/flask/templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 930, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 883, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 857, in _load_template
template = self.loader.load(self, name, globals)
File "/usr/local/lib/python3.8/site-packages/jinja2/loaders.py", line 115, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python3.8/site-packages/flask/templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python3.8/site-packages/flask/templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: reviewform.html
【问题讨论】:
-
您是否在任何地方设置 FLASK_APP 环境变量?
-
错误现在不同了
jinja2.exceptions.TemplateNotFound: reviewform.html检查模板,因为它抱怨没有找到。 -
我想你可以再问一个问题,因为这个错误与
docker无关?泊坞窗问题已解决;) -
我明白了,然后将
flaskapp.py复制到正确的路径ADD flaskapp.py /tmp,它应该可以工作。
标签: python docker flask pytorch jinja2