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
Mastra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.