| # Use an official lightweight Python image | |
| FROM python:3.11-slim | |
| # Create a non-root user (UID 1000 is required by Hugging Face Spaces) | |
| RUN useradd -m -u 1000 user | |
| # Set the working directory | |
| WORKDIR /app | |
| # Install system dependencies (required for Pillow and image processing) | |
| RUN apt-get update && apt-get install -y \ | |
| build-essential \ | |
| libpq-dev \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Copy the requirements file from the backend folder first | |
| COPY keystone-backend/requirements.txt . | |
| # Install Python dependencies | |
| RUN pip install --no-cache-dir -r requirements.txt | |
| # Copy the contents of the backend folder into the container | |
| # and assign ownership to the non-root user | |
| COPY --chown=user:user keystone-backend/ . | |
| # Switch to the non-root user | |
| USER user | |
| # Hugging Face Spaces route web traffic to port 7860 | |
| EXPOSE 7860 | |
| # Health check: verify the bucket at /data is mounted before reporting healthy. | |
| # If the bucket is missing, the Space will show as unhealthy immediately. | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \ | |
| CMD python -c "import os, sys; sys.exit(0 if os.path.exists('/data') else 1)" | |
| # Start the FastAPI server (it will run main.py from the copied backend files) | |
| # NOTE: main.py will raise a RuntimeError at startup if /data is not mounted, | |
| # ensuring photos are NEVER saved to ephemeral container storage. | |
| CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"] |