我正在设置一个flask开发环境,并希望我的flask服务器检测代码中的更改并自动重新加载。
我的项目结构是:
python-flask
|--docker-compose.yml
|--flask
| |--Dockerfile
| |--requirements.txt
| |--app.ini
| |--run.py
| |--app
| |--__init__.py
| |--app.py
|--nginx
|--Dockerfile
|--nginx.conf
app.py
的代码import os
from flask import Flask, session, g
app = Flask(__name__)
@app.route("/")
def index():
# Use os.getenv("key") to get environment variables
app_name = os.getenv("APP_NAME")
if app_name:
return f"Hello from {app_name} running in a Docker container behind Nginx!"
return "Hello from Flask"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
这是我的 docker-compose.yml
version: "3.7"
services:
flask:
build: ./flask
container_name: flask
restart: always
volumes:
- ./flask/app:/app
environment:
- APP_NAME=MyFlaskApp
expose:
- 8080
nginx:
build: ./nginx
container_name: nginx
restart: always
ports:
- "80:80"
如Auto reloading flask server on Docker中所述,我在我的app.ini中添加了py-autoreload = 1
。这是我的 app.ini
[uwsgi]
wsgi-file = run.py
callable = app
socket = :8080
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
die-on-term = true
py-autoreload = 1
当我运行docker-compose up --build
时,flask返回错误:
Starting nginx ... done
Attaching to flask, nginx
flask | realpath() of app.ini failed: No such file or directory [core/utils.c line 3654]
我不确定如何解决此错误。我尝试将docker-compose.yml中的volume
更改为./app:/app
,但是错误仍然存在