| # --- Stage 1: Build Stage --- | |
| # Use a full Python image to build our dependencies. | |
| FROM python:3.11-slim as builder | |
| # Set the working directory. | |
| WORKDIR /app | |
| # Install poetry for dependency management. | |
| # Using poetry is a best practice for managing dependencies. | |
| RUN pip install poetry | |
| # Copy only the files needed for dependency installation. | |
| # This leverages Docker's layer caching. | |
| COPY pyproject.toml poetry.lock ./ | |
| # Install dependencies. | |
| # --no-root: Don't install the project itself, only the dependencies. | |
| # --no-dev: Exclude development dependencies. | |
| RUN poetry install --no-root --no-dev | |
| # --- Stage 2: Runtime Stage --- | |
| # Use a slim Python image for the final container. | |
| FROM python:3.11-slim | |
| # Set the working directory. | |
| WORKDIR /app | |
| # Copy the installed dependencies from the builder stage. | |
| COPY --from=builder /app/.venv /.venv | |
| # Set the PATH to include the virtual environment's bin directory. | |
| ENV PATH="/app/.venv/bin:$PATH" | |
| # Copy the application code. | |
| COPY . . | |
| # Expose the port Gunicorn will run on. | |
| EXPOSE 8000 | |
| # The command to run the application using Gunicorn. | |
| CMD ["gunicorn", "-c", "gunicorn_config.py", "main:app"] |