Auto reloading flask server on Docker Auto reloading flask server on Docker flask flask

Auto reloading flask server on Docker


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:alpineEXPOSE 5000WORKDIR appCOPY * /app/RUN pip install -r requirements.txtCMD python app.py


I managed to achieve flask auto reload in docker using docker-compose with the following config:

version: "3"services:  web:    build: ./web    entrypoint:      - flask      - run      - --host=0.0.0.0    environment:      FLASK_DEBUG: 1      FLASK_APP: ./app.py    ports: ['5000:5000']    volumes: ['./web:/app']

You have to manually specify environment variables and entrypoint in the docker compose file in order to achieve auto reload.


Assuming your file structure is the below:

enter image description here

Dockerfile: (note WORKING DIR)

FROM python:3.6.5-slimRUN mkdir -p /home/project/bottleWORKDIR /home/project/bottle COPY requirements.txt .RUN pip install --upgrade pip --no-cache-dir -r requirements.txtCOPY . .CMD ["python", "app.py"]

Docker Compose:

version: '3'services:  web:    container_name: web    volumes:      - './web:/home/project/bottle/'  <== Local Folder:WORKDIR    build: ./web    ports:      - "8080:8080"