FROM python:3.11-slim ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 \ PORT=7860 \ MEDIA_ROOT=/home/user/app/media # Install required system packages for OpenCV RUN apt-get update && apt-get install -y --no-install-recommends \ libgl1 \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender1 \ && rm -rf /var/lib/apt/lists/* # Set up a new user called "user" with user ID 1000 (Required for Hugging Face Spaces) RUN useradd -m -u 1000 user USER user ENV HOME=/home/user \ PATH=/home/user/.local/bin:$PATH WORKDIR $HOME/app # Copy requirements separately to cache the layer COPY --chown=user requirements.txt ./ # Install python dependencies RUN pip install --upgrade pip && \ pip install -r requirements.txt # Copy project files with proper ownership COPY --chown=user . . # Create necessary directories with proper user permissions RUN mkdir -p $HOME/app/staticfiles $HOME/app/media EXPOSE 7860 # Run migrations, collect static files, seed catalog only when empty, then start Gunicorn. # RUN_RESET_CATALOG=1 can still be used for a full reset when explicitly requested. CMD ["sh", "-c", "python manage.py migrate && python manage.py collectstatic --noinput && if [ \"${RUN_RESET_CATALOG:-0}\" = \"1\" ]; then python manage.py reset_catalog; elif ! python manage.py shell -c \"from fitting_system.models import Product; import sys; sys.exit(0 if Product.objects.exists() else 1)\"; then python manage.py reset_catalog; fi && python manage.py create_admin && gunicorn config.wsgi:application --bind 0.0.0.0:${PORT:-7860} --workers 2 --timeout 120"]