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.
Here's my tree
├── docker-compose.yml └── web ├── Dockerfile ├── app.py ├── crawler.py └── requirements.txt
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')
and docker-compose
version: '2' services: web: build: ./web ports: - "5000:5000" volumes: - ./web:/code
Please give me some advice. Thank you in advance.
In this tutorial, we built a simple Flask app and containerized it with Docker. We also deployed the created and pushed the image to Docker Hub as well as the containerized application to Heroku. Just as demonstrated with a Flask application, you can also explore how to containerize other applications here.
If you're using the app. run() method instead of the flask run command, pass debug=True to enable debug mode. Tracebacks are also printed to the terminal running the server, regardless of development mode.
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:
Here is the docker-compose.yaml
version: "3" services: web: build: ./web ports: ['5000:5000'] volumes: ['./web:/app']
And here the Dockerfile
:
FROM python:alpine EXPOSE 5000 WORKDIR app COPY * /app/ RUN pip install -r requirements.txt CMD python app.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With