问题描述
我希望我的flask服务器检测代码中的更改并自动重新加载。
我正在docker容器上运行它。
每当我更改某些内容时,都必须重新构建容器。我不知道哪里错了。这是我第一次使用烧瓶。
I want my flask server to detect changes in code and reload automatically.I'm running this on docker container.Whenever I change something, I have to build and up again the container. I have no idea where's wrong. This is my first time using flask.
这是我的树
├── docker-compose.yml
└── web
├── Dockerfile
├── app.py
├── crawler.py
└── requirements.txt
和代码(app.py)
and code(app.py)
from flask import Flask
import requests
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello Flask!!'
if __name__ == '__main__':
app.run(debug = True, host = '0.0.0.0')
和docker-compose
and docker-compose
version: '2'
services:
web:
build: ./web
ports:
- "5000:5000"
volumes:
- ./web:/code
请给我一些建议。
推荐答案
在调试模式下,Flask支持重新加载代码,就像您已经完成的一样。问题是该应用程序正在容器上运行,这使它与正在开发的真实源代码隔离。无论如何,您可以在运行容器和主机之间的 docker-compose.yaml
上共享卷,就像这样:
Flask supports code reload when in debug mode as you've already done. The problem is that the application is running on a container and this isolates it from the real source code you are developing. Anyway, you can share the source between the running container and the host with volumes on your docker-compose.yaml
like this:
这是 docker-compose.yaml
version: "3"
services:
web:
build: ./web
ports: ['5000:5000']
volumes: ['./web:/app']
这里是 Dockerfile
:
FROM python:alpine
EXPOSE 5000
WORKDIR app
COPY * /app/
RUN pip install -r requirements.txt
CMD python app.py
这篇关于在Docker上自动重新加载Flask服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!