![google-labs-jules[bot]](/assets/img/avatar_default.png)
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.
75 lines
2.0 KiB
Docker
75 lines
2.0 KiB
Docker
# Multi-stage build for production - optimized for size
|
|
FROM python:3.11-slim AS builder
|
|
|
|
# Set environment variables
|
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
|
PYTHONUNBUFFERED=1 \
|
|
PIP_NO_CACHE_DIR=1 \
|
|
PIP_DISABLE_PIP_VERSION_CHECK=1
|
|
|
|
# Install build dependencies
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
gcc \
|
|
g++ \
|
|
libpq-dev \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Install Python dependencies
|
|
COPY requirements.txt .
|
|
RUN pip install --user --no-cache-dir -r requirements.txt
|
|
|
|
# Production stage - minimal image
|
|
FROM python:3.11-slim AS production
|
|
|
|
# Set environment variables
|
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
|
PYTHONUNBUFFERED=1 \
|
|
PATH=/home/appuser/.local/bin:$PATH
|
|
|
|
# Install only runtime dependencies
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
libpq5 \
|
|
curl \
|
|
&& rm -rf /var/lib/apt/lists/* \
|
|
&& apt-get clean
|
|
|
|
# Create non-root user
|
|
RUN groupadd -g 1001 appuser && \
|
|
useradd -u 1001 -g appuser -m appuser
|
|
|
|
# Copy Python packages from builder stage
|
|
COPY --from=builder /root/.local /home/appuser/.local
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy only necessary application files (be selective)
|
|
COPY --chown=appuser:appuser app/ ./app/
|
|
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
|
|
|
|
# Switch to non-root user
|
|
USER appuser
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
|
|
CMD curl -f http://localhost:8000/health || exit 1
|
|
|
|
# Expose port
|
|
EXPOSE 8000
|
|
|
|
# Production command
|
|
ENTRYPOINT ["/app/entrypoint.sh"]
|
|
CMD ["uvicorn", "app.main:app", \
|
|
"--host", "0.0.0.0", \
|
|
"--port", "8000", \
|
|
"--workers", "4", \
|
|
"--access-log", \
|
|
"--log-level", "info"] |