在Docker中运行的Flask应用找不到模板

时间:2018-12-25 07:38:27

标签: python docker flask

使用docker-compose运行前端和后端时,我正在开发许多微服务和前端,前端无法从模板文件夹中找到home.html。当我将应用程序作为常规Flask应用程序运行时,它运行良好。只有合并Docker时,我才会遇到这个问题。

├── backend
│   ├── app.py
│   ├── Dockerfile
│   └── requirements.txt
├── deployment.yaml
├── docker-compose.yaml
├── frontend
│   ├── app.py
│   ├── Dockerfile
│   ├── requirements.txt
│   └── templates
│       ├── home.html
│       ├── includes
│       │   ├── _formhelpers.html
│       │   ├── _messages.html
│       │   └── _navbar.html
│       └── layout.html
└── README.md

frontend / app.py

from flask import Flask, jsonify, render_template
import requests
import json
import os

app = Flask(__name__)

@app.route("/hello_world")
def hello_world():
be_host = os.getenv('BACKEND_SERVICE_HOST', 'backend')
be_port = os.getenv('BACKEND_SERVICE_PORT', '5000')
url = 'http://{}:{}/hello_world'.format(be_host, be_port)
try:
    res = requests.get(url)
except Exception:
    return "Error with {}".format(url)
dictFromServer = res.json()
return render_template('home.html', msg=dictFromServer['message'])


if __name__=='__main__':
    app.run(debug=True, host='0.0.0.0')

Dockerfile

FROM python:3.6-alpine
WORKDIR /app
COPY requirements.txt /app
COPY app.py /app
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app.py"]

requirements.txt

Flask
requests
jsonify

1 个答案:

答案 0 :(得分:1)

您尚未将模板文件夹复制到您的Docker容器中:

...
COPY app.py /app
COPY templates /app/templates
...