From 4540ad359e8a21a0b9fe56609201169f4d0fa4ff Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 1 Jun 2025 17:10:40 +0000 Subject: [PATCH] Fix(docker): Run Alembic migrations on container startup This commit introduces changes to ensure that Alembic database migrations are automatically applied when the backend Docker container starts. Key changes: - Added `be/entrypoint.sh`: This script first runs `alembic upgrade head` to apply any pending migrations and then executes the main container command (e.g., starting Uvicorn). - Modified `be/Dockerfile`: - The `entrypoint.sh` script is copied into the image and made executable. - The Docker `ENTRYPOINT` is set to this script, ensuring migrations run before the application starts. - Updated `docker-compose.yml`: - The `DATABASE_URL` for the `backend` service has been set to the Neon database URL you provided. - Verified `be/alembic/env.py`: Confirmed that it correctly sources the `DATABASE_URL` from environment variables for Alembic to use. These changes address the issue where migrations were not being run, preventing the application from starting correctly. --- be/Dockerfile | 3 +++ be/entrypoint.sh | 10 ++++++++++ docker-compose.yml | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100755 be/entrypoint.sh diff --git a/be/Dockerfile b/be/Dockerfile index 7365f1a..1dda81e 100644 --- a/be/Dockerfile +++ b/be/Dockerfile @@ -49,6 +49,8 @@ COPY --chown=appuser:appuser alembic/ ./alembic/ COPY --chown=appuser:appuser alembic.ini ./ COPY --chown=appuser:appuser *.py ./ COPY --chown=appuser:appuser requirements.txt ./ +COPY --chown=appuser:appuser entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh # Create logs directory RUN mkdir -p /app/logs && chown -R appuser:appuser /app @@ -64,6 +66,7 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \ EXPOSE 8000 # Production command +ENTRYPOINT ["/app/entrypoint.sh"] CMD ["uvicorn", "app.main:app", \ "--host", "0.0.0.0", \ "--port", "8000", \ diff --git a/be/entrypoint.sh b/be/entrypoint.sh new file mode 100755 index 0000000..01d6629 --- /dev/null +++ b/be/entrypoint.sh @@ -0,0 +1,10 @@ +#!/bin/sh +set -e + +# Run database migrations +echo "Running database migrations..." +alembic upgrade head + +# Execute the command passed as arguments to this script +echo "Starting application..." +exec "$@" diff --git a/docker-compose.yml b/docker-compose.yml index 4e9ed75..88c6f5e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,7 +33,7 @@ services: # Pass the database URL to the backend container # Uses the service name 'db' as the host, and credentials defined above # IMPORTANT: Use the correct async driver prefix if your app needs it! - - DATABASE_URL=xxx + - DATABASE_URL=postgresql+asyncpg://mitlist_owner:npg_p0SkmyJ6BPWO@ep-small-sound-a9ketcef-pooler.gwc.azure.neon.tech/testnewmig - GEMINI_API_KEY=xxx - SECRET_KEY=xxx # Add other environment variables needed by the backend here