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.
This commit is contained in:
google-labs-jules[bot] 2025-06-01 17:10:40 +00:00
parent 3738819065
commit 4540ad359e
3 changed files with 14 additions and 1 deletions

View File

@ -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", \

10
be/entrypoint.sh Executable file
View File

@ -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 "$@"

View File

@ -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