Home

Live-updating Flask in Docker

It is entirely possible to update your Python Flask application and see those changes reflected at a related domain—without the cumbersome process of stopping and recreating the Docker container. This technique, often called “hot-reloading,” is a cornerstone of efficient development workflows and can even be leveraged in some production scenarios.

Key Components for Hot-Reloading:


Development Environment: Leveraging Flask’s Debug Mode

For development, you can use Flask’s built-in reloader. When you run Flask in debug mode, the server restarts automatically whenever a Python file is modified.

Example docker-compose.yml for Development:


services:  
  web:  
    build: .  
    ports:  
      - "5000:5000"  
    environment:  
      - FLASK_ENV=development  
      - FLASK_APP=app.py  
      - FLASK_DEBUG=1  
    command: python app.py  
    volumes: 
      - .:/app 
    develop:  
      watch:  
        - action: sync  
          path: .  
          target: /app  
        - action: rebuild  
          path: requirements.txt  

Example Dockerfile:

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["flask", "run", "--host=0.0.0.0"]

How it works:


Production Environment: Gunicorn and uWSGI for Robust Reloading

For production, use a more resilient WSGI server. Both Gunicorn and uWSGI offer mechanisms for reloading your application without downtime.

Using Gunicorn with --reload

Gunicorn provides a --reload flag that watches for file changes and gracefully reloads worker processes.

Example docker-compose.yml for Production:

version: '3.8'
services:
  web:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - .:/app
    command: gunicorn --bind 0.0.0.0:8000 --workers 3 --reload your_app_module:app

Example Dockerfile:

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "your_app_module:app"]

Using uWSGI with Hot-Reloading

uWSGI supports hot-reloading via options like --touch-reload and py-autoreload.

Example uwsgi.ini:

[uwsgi]
module = your_app_module:app
master = true
processes = 4
socket = :8000
chmod-socket = 660
vacuum = true
die-on-term = true
touch-reload = /app/reload_trigger
py-autoreload = 1

Triggering the Update

When your code is mounted as a Docker volume and your server is configured for reloading, any change to the source code on your host is detected by the server inside the container, which then reloads the application. This allows you to edit your Python files and see changes live, without restarting the Docker container.


Summary Table: Development vs. Production Hot-Reloading

Environment Server Reload Mechanism Docker Volume Needed Recommended Use
Development Flask dev Debug mode auto-reload Yes Local development
Production Gunicorn --reload flag Yes Staging/testing
Production uWSGI touch-reload/py-autoreload Yes Advanced/production

By strategically combining Docker volumes with the reloading capabilities of modern WSGI servers, you can create an efficient and dynamic workflow for developing and updating your Flask applications—eliminating the need for constant container restarts and streamlining both development and deployment.

Tags: Docker, Flask, Python