Hot reload flask 2.3.3 running in a docker container not working

I’m working inside a flask micro service which is running inside a docker container.

The app structure is

├── README.md
├── pricing-compose-dev.yml
├── pricing-compose.yml
└── pricing_gateway
    ├── Dockerfile-slim-bullseye-python312
    ├── app_pricing_gateway.py
    ├── debugger.py
    ├── pywsgi.py
    ├── requirements.txt
    .
    .
    .

Here, I use the pricing-compose-dev.yml to run my local environment. It seems like this

version: "3.8"

services:

  pricing_gateway:
    container_name: pricing_gateway
    init: true
    restart: unless-stopped
    ports:
      - "6062:3055"
      - "10001:10001"
    build:
      context: pricing_gateway
      dockerfile: Dockerfile-slim-bullseye-python312
    entrypoint: flask --app pywsgi.py run --debug
    env_file: .env
    volumes:
      - ./pricing_gateway:/app
    
  pricing_postgres:
    container_name: pricing_postgres
    image: postgres:14-alpine
    ports:
      - "6435:5432"
    expose:
      - "6435"
    volumes:
      - ~/pricing_app/postgres:/var/lib/postgresql/data 
    environment:
      - POSTGRES_PASSWORD=XXXX
      - POSTGRES_USER=XXXX
      - POSTGRES_DB=XXXX

The Dockerfile-slim-bullseye-python312 file contains

FROM python:3.12.0-slim-bullseye

WORKDIR /app

RUN apt-get update && apt-get install -y git
COPY . .
RUN pip3 install --no-cache-dir -r requirements.txt
RUN pip3 install debugpy

EXPOSE 3054

The entrypoint run the code inside pywsgi.py, that is

from gevent import monkey
monkey.patch_all()
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from app_pricing_gateway import app
import os

os.environ.update()
server = WSGIServer( ("0.0.0.0", 3054), app, handler_class=WebSocketHandler,)
server.serve_forever()

And it runs the code inside app_pricing_gateway

from flask import Flask, Blueprint
from graphql_server.flask import GraphQLView
from schemas.schema_protected import schema_protected
from system_setup import api as private_api
from carecart_pricing_ctx_database_sqlalchemy_14.models.base import db_session
from debugger import initialize_flask_server_debugger_if_needed

initialize_flask_server_debugger_if_needed()

app = Flask(__name__)

api_public = Blueprint('public_api', __name__)
app.register_blueprint(private_api, url_prefix='/protected/pricing')

app.add_url_rule('/protected/pricing/gateway_graphql', view_func=GraphQLView.as_view(
   'graphqlProtected',
   schema=schema_protected,
   graphiql=True,
))

@app.teardown_appcontext
def shutdown_session(exception=None):
    db_session.remove()

if __name__ == '__main__':
    app.run()

It use the function initialize_flask_server_debugger_if_needed which is defined in debugger.py

from os import getenv

def initialize_flask_server_debugger_if_needed():
    if getenv("DEBUGGER") == "True":
        import multiprocessing
         
        if multiprocessing.current_process().pid > 1:
            import debugpy

            debugpy.listen(("0.0.0.0", 10001))
            print("⏳ VS Code debugger can now be attached, press F5 in VS Code ⏳", flush=True)
            debugpy.wait_for_client()
            print("???? VS Code debugger attached, enjoy debugging ????", flush=True)

At least, to debug my application I use this configuration in my launch.json

    {
      "name": "Attach to Pricing",
      "type": "debugpy",
      "request": "attach",
      "connect": {
        "host": "127.0.0.1",
        "port": 10001
      },
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}/Pricing-Context-CareCart/pricing_gateway",
          "remoteRoot": "/app"
        }
      ]
    },

P.D: my requirements.txt file have the following libs

Flask = 2.3.3
.
.
.
debug-py

Everything works well. I can run my container, attach my debugger and debug without any problems my endpoints.

The problem happens when I change something in the code. I want it to automatically reload, and not need to make the build again manually.

I read that with the option --debug should be enough, but it doesn’t seems to work.

I also tried adding the environment variables

FLASK_DEBUG=1
FLASK_ENV=development

New contributor

Mastra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật