FROM python:3.9-slim AS builder WORKDIR /app COPY ./pyproject.toml . COPY ./poetry.lock . # Install build dependencies RUN apt-get update \ && apt-get install -y --no-install-recommends build-essential \ && pip install --upgrade pip poetry \ && poetry config virtualenvs.create false \ && poetry install --no-interaction --no-ansi FROM python:3.9-slim WORKDIR /app # Install runtime dependencies RUN apt-get update \ && apt-get install -y --no-install-recommends libpq5 redis-server \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* # Install PostgreSQL client RUN apt-get update \ && apt-get install -y --no-install-recommends postgresql-client \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* # Switch to the root user USER root # Copy the built virtual environment from the builder stage COPY --from=builder /usr/local /usr/local # Copy the rest of the backend files to the container COPY ./ . # Initialize PostgreSQL data directory and set permissions RUN mkdir -p /usr/local/pgsql/data \ && chown -R postgres:postgres /usr/local/pgsql/data \ && su -c "/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data" postgres # Start Redis server and PostgreSQL CMD ["bash", "-c", "redis-server --daemonize yes && su -c '/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data -l logfile' postgres && uvicorn app:app --host 0.0.0.0 --port 7860"]