对docker来说是新手,并尝试运行flask mysql应用程序,但得到jinja2.exceptions.TemplateNotFound:index.html。如果我在docker之外运行python app.py
则没有错误
目录结构
-docker-compose.yml
-app
-templates
-index.html
-app.py
-Dockerfile
-requirements.txt
-db
-init.sql
docker-compose.yml
version: "2"
services:
app:
build: ./app
links:
- db
ports:
- "5000:5000"
db:
image: mysql:5.7
ports:
- "32000:3306"
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./db:/docker-entrypoint-initdb.d/:ro
Dockerfile
FROM python:3.6
EXPOSE 5000
WORKDIR /app
COPY requirements.txt /app
RUN pip install -r requirements.txt
ENV IN_DOCKER_CONTAINER Yes
COPY app.py /app
CMD python app.py
requirements.txt:
Flask==1.0.2
Jinja2==2.10
gunicorn==19.6.0
flask-mysql
我的app.py的一部分:
@app.route('/')
def index():
conn = mysql.connect()
cursor = conn.cursor()
try:
query = '''SELECT * from favorite_colors'''
cursor.execute(query)
data = cursor.fetchall()
except Exception as e:
return str(e)
finally:
cursor.close()
conn.close()
return render_template('index.html', MyExampleVar=str(data))
答案 0 :(得分:1)
您的Dockerfile
仅将requirements.txt
和app.py
复制到图像中。为了使被docker化的app.py
可以访问templates
及其内容,还需要添加以下行来复制templates
:
COPY templates /app/