# STAGE 1: Build Stage FROM python:3.11-slim AS builder WORKDIR /app # --- CACHE BUSTING MECHANISM --- # Define a build argument that will be passed during the docker build command. # Change the number (e.g., from 1 to 2) every time you build if the installation fails. ARG BUILD_ID=1 # Install build dependencies for the Python packages RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ git \ && rm -rf /var/lib/apt/lists/* # Create a virtual environment and set it in the PATH RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # Copy the requirements file and install dependencies COPY requirements.txt . # Use the BUILD_ID as an ENV variable. When this ENV value changes, # Docker's cache for the following RUN instruction (pip install) is invalidated. ENV FORCE_INSTALL=${BUILD_ID} RUN pip install --no-cache-dir -r requirements.txt # -------------------------------------------------------------------------- # STAGE 2: Final Stage FROM python:3.11-slim WORKDIR /app # Install runtime dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ && rm -rf /var/lib/apt/lists/* # Add a non-root user and group RUN addgroup --system app && adduser --system --group app # CRITICAL FIX: Ensure the working directory (/app) is owned by the app user RUN chown app:app /app # Copy the virtual environment, source code, and data file with correct ownership COPY --from=builder --chown=app:app /opt/venv /opt/venv COPY --chown=app:app ./src ./src COPY --chown=app:app ./training_data.csv . # Switch to the non-root user USER app # Set up environment variables and create the cache directory as the 'app' user ENV PATH="/opt/venv/bin:$PATH" ENV HF_HOME="/tmp/hf_cache" RUN mkdir -p /app/hf_cache # --- Container configuration --- EXPOSE 8501 HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health # --- Streamlit configuration and Entrypoint --- ENV STREAMLIT_SERVER_HEADLESS=true ENV STREAMLIT_BROWSER_GATHERUSAGETSTATS=false ENTRYPOINT ["streamlit", "run", "src/app.py", "--server.port=8501", "--server.address=0.0.0.0"]