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 install postgresql \ && 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 # Create the postgres user and group RUN groupadd -r postgres && useradd --no-log-init -r -g postgres postgres # 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 ./ . # Switch back to a non-root user for security USER appuser # Install additional Python dependencies #RUN pip install -U -q pyngrok ipython psycopg2 alembic # Create PostgreSQL users and databases in the background RUN service redis-server start \ && service postgresql start \ && sudo -u postgres psql -c "CREATE USER postadmin WITH PASSWORD 'postpass';" \ && sudo -u postgres psql -c "CREATE DATABASE siksalaya;" \ && sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE sikshyalaya TO postadmin;" # Start the FastAPI app using Uvicorn CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]