Fixing Duplicate Table Error in Docker Compose with FastAPI

Fresh DB. Clean slate. First ever docker-compose up. And it throws a duplicate table error. How?? There's nothing there. I created nothing. It's literally the first run. ... Turns out, the fastapi app was getting spun up like this > command: gunicorn app.main:app -w 4 .... Looking closely, gunicorn was spinning up 4 workers simultaneously. Every single one of them called create_tables() on boot. Race condition on a clean slate! 🏎️ 💨 Root cause: multi-worker setup + app-level table creation = workers racing each other Fixes: • Dev → switch to uvicorn (single worker, problem gone) • Prod → run an init container or entrypoint script to handle table creation before gunicorn starts • Code-level → make table creation idempotent, handle duplicates gracefully Building a side project is just debugging with extra steps 🥲 #Docker #Python #FastAPI #SideProject #DevLog #BuildingInPublic

To view or add a comment, sign in

Explore content categories