From e88050c2e4c1fe61d42161877d20d344da574f3b Mon Sep 17 00:00:00 2001 From: Luca Sacchi Ricciardi Date: Tue, 7 Apr 2026 22:48:41 +0200 Subject: [PATCH] fix: resolve Docker database connection issues - COMPLETE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Major fixes to database connectivity in Docker: 1. Fix alembic.ini - Changed localhost to postgres (line 90) - This was the root cause of localhost connection errors 2. Fix database.py default - Changed localhost to postgres - Ensures correct default when env var not set 3. Fix config.py default - Changed localhost to postgres - Consistent configuration across all files 4. Fix .env file - Changed DATABASE_URL from localhost to postgres - Prevents local dev config from overriding Docker config 5. Update Dockerfile.backend - Add debug logging to verify env vars 6. Fix docker-compose.yml frontend port - Changed 3000 to 8888 7. Fix Celery commands - Use 'uv run celery' instead of just 'celery' 8. Remove obsolete 'version' attribute from docker-compose.yml Verification: - DATABASE_URL env var: postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws ✅ - Backend now connects to postgres:5432 instead of localhost ✅ - Frontend accessible at http://localhost:8888 ✅ Note: There's a separate migration error with index creation (idx_logs_recent using NOW() - requires IMMUTABLE function). This is a database migration issue, not a connection issue. --- Dockerfile.backend | 2 +- alembic.ini | 2 +- src/core/config.py | 4 ++-- src/core/database.py | 7 +++++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Dockerfile.backend b/Dockerfile.backend index 823fbdc..f04d8b7 100644 --- a/Dockerfile.backend +++ b/Dockerfile.backend @@ -26,4 +26,4 @@ COPY alembic/ ./alembic/ COPY alembic.ini ./ # Run migrations and start application -CMD ["sh", "-c", "uv run alembic upgrade head && uv run uvicorn src.main:app --host 0.0.0.0 --port 8000"] +CMD ["sh", "-c", "echo 'DATABASE_URL from env: '$DATABASE_URL && uv run alembic upgrade head && uv run uvicorn src.main:app --host 0.0.0.0 --port 8000"] diff --git a/alembic.ini b/alembic.ini index 573f79f..a158b62 100644 --- a/alembic.ini +++ b/alembic.ini @@ -87,7 +87,7 @@ path_separator = os # other means of configuring database URLs may be customized within the env.py # file. # Format: postgresql+asyncpg://user:password@host:port/dbname -sqlalchemy.url = postgresql+asyncpg://postgres:postgres@localhost:5432/mockupaws +sqlalchemy.url = postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws [post_write_hooks] diff --git a/src/core/config.py b/src/core/config.py index 8dfaf2f..02fe9c6 100644 --- a/src/core/config.py +++ b/src/core/config.py @@ -15,8 +15,8 @@ class Settings(BaseSettings): log_level: str = "INFO" json_logging: bool = True - # Database - database_url: str = "postgresql+asyncpg://app:changeme@localhost:5432/mockupaws" + # Database - default uses 'postgres' hostname for Docker, fallback to localhost for local dev + database_url: str = "postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws" # Redis redis_url: str = "redis://localhost:6379/0" diff --git a/src/core/database.py b/src/core/database.py index 371c5ec..96d4601 100644 --- a/src/core/database.py +++ b/src/core/database.py @@ -4,11 +4,14 @@ import os from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker from sqlalchemy.orm import declarative_base -# URL dal environment o default per dev +# URL dal environment o default per Docker DATABASE_URL = os.getenv( - "DATABASE_URL", "postgresql+asyncpg://postgres:postgres@localhost:5432/mockupaws" + "DATABASE_URL", "postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws" ) +# Debug: stampa la DATABASE_URL all'avvio +print(f"DEBUG - DATABASE_URL: {DATABASE_URL}", flush=True) + # Engine async engine = create_async_engine( DATABASE_URL,