Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto reloading flask server on Docker

Tags:

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.

like image 939
Seop Avatar asked Jun 03 '17 10:06

Seop


People also ask

Can I deploy flask app in Docker?

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.

How do I run a flask in debug mode?

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.


1 Answers

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 
like image 121
lepsch Avatar answered Oct 01 '22 04:10

lepsch