FROM python:3.10-slim # Set working directory WORKDIR /app # Install system dependencies including security tools RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ g++ \ make \ git \ curl \ libseccomp2 \ pkg-config \ libffi-dev \ libssl-dev \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ && rm -rf /tmp/* /var/tmp/* # Create a non-root user with restricted shell RUN groupadd -r -g 1000 botuser && \ useradd -r -m -u 1000 -g botuser -s /usr/sbin/nologin botuser && \ mkdir -p /app/bots /app/data /app/templates /app/cache /app/logs /app/uploads /app/cache/pip && \ chmod 755 /app # Copy requirements first for better caching COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Clean up pip cache RUN rm -rf /root/.cache/pip # Copy application code COPY . . # Set ownership after all files are copied RUN chown -R botuser:botuser /app && \ chmod -R 755 /app # Set secure environment variables ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PYTHONHASHSEED=random \ GRADIO_SERVER_NAME="0.0.0.0" \ GRADIO_SERVER_PORT=7860 \ UVICORN_HOST="0.0.0.0" \ UVICORN_PORT=8000 \ HF_HOME=/app/data/huggingface \ BOTS_DIR=/app/bots \ PIP_CACHE_DIR=/app/cache/pip \ MAX_MEMORY_MB=512 \ MAX_CPU_TIME=30 # Switch to non-root user USER botuser # Create user directories with proper permissions RUN mkdir -p /app/bots /app/data /app/templates /app/cache/pip /app/logs /app/uploads # Expose ports EXPOSE 7860 8000 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD curl -f http://localhost:7860/ || exit 1 # Run the application CMD ["python", "app.py"]