我正在尝试将使用flask的应用程序容器化。当我正常运行该应用程序时,它运行良好,但是当我在容器中启动该应用程序时,它会抛出jinja2.exceptions.TemplateNotFound:index.html。
我尝试了多个答案,例如Docker flask - jinja2.exceptions.TemplateNotFound: index.html,这表明应用程序目录中没有模板文件夹,而我的却有
-myApp
-templates
- index.html
-flaskapp.py
-Dockerfile
-docker-compose.yml
FROM python:3.7-slim
copy . /app
COPY templates /app/
Expose 3030
RUN pip3 install Flask
CMD ["python3","/app/flaskapp.py"]
import Flask
@app.route('/')
def index():
return render_template('index.html')
当我尝试访问localhost:3030时
image_server_1 | * Serving Flask app "flask_opencv_streamer.streamer_3030" (lazy loading) image_server_1 | * Environment: production image_server_1 | WARNING: This is a development server. Do not use it in a production deployment. image_server_1 | Use a production WSGI server instead. image_server_1 | * Debug mode: off image_server_1 | * Running on http://0.0.0.0:3030/ (Press CTRL+C to quit) image_server_1 | [2019-06-24 12:15:39,255] ERROR in app: Exception on / [GET] image_server_1 | Traceback (most recent call last): image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2311, in wsgi_app image_server_1 | response = self.full_dispatch_request() image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1834, in full_dispatch_request image_server_1 | rv = self.handle_user_exception(e) image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1737, in handle_user_exception image_server_1 | reraise(exc_type, exc_value, tb) image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 36, in reraise image_server_1 | raise value image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1832, in full_dispatch_request image_server_1 | rv = self.dispatch_request() image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1818, in dispatch_request image_server_1 | return self.view_functions[rule.endpoint](**req.view_args) image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask_opencv_streamer/streamer.py", line 213, in decorated image_server_1 | return func(*args, **kwargs) image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask_opencv_streamer/streamer.py", line 89, in index image_server_1 | return render_template("index.html") image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask/templating.py", line 134, in render_template image_server_1 | return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list), image_server_1 | File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 869, in get_or_select_template image_server_1 | return self.get_template(template_name_or_list, parent, globals) image_server_1 | File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 830, in get_template image_server_1 | return self._load_template(name, self.make_globals(globals)) image_server_1 | File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 804, in _load_template image_server_1 | template = self.loader.load(self, name, globals) image_server_1 | File "/usr/local/lib/python3.7/site-packages/jinja2/loaders.py", line 113, in load image_server_1 | source, filename, uptodate = self.get_source(environment, name) image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask/templating.py", line 58, in get_source image_server_1 | return self._get_source_fast(environment, template) image_server_1 | File "/usr/local/lib/python3.7/site-packages/flask/templating.py", line 86, in _get_source_fast image_server_1 | raise TemplateNotFound(template) image_server_1 | jinja2.exceptions.TemplateNotFound: index.html
答案 0 :(得分:0)
问题是烧瓶在根目录中查找,我认为它会递归显示,但看起来好像没有
所以我添加了
WORKDIR /app
在Dockerfile中的CMD命令之前,它运行良好