# Start with a slim and secure Python base image FROM python:3.9-slim # --- Environment Variables --- # Prevents Python from writing .pyc files to disc ENV PYTHONDONTWRITEBYTECODE 1 # Ensures Python output is sent straight to the terminal without buffering ENV PYTHONUNBUFFERED 1 # Set the MPLCONFIGDIR to a writable directory to avoid matplotlib warnings ENV MPLCONFIGDIR /tmp/matplotlib # --- System Dependencies --- # First, update the package lists and install system libraries required by OpenCV. # Combining these into a single RUN layer reduces image size. # libglib2.0-0 is essential to fix the "libgthread-2.0.so.0" error. RUN apt-get update && \ apt-get install -y --no-install-recommends \ libgl1-mesa-glx \ libglib2.0-0 && \ # Clean up the apt cache to reduce image size rm -rf /var/lib/apt/lists/* # --- Python Dependencies --- # Set the working directory WORKDIR /app # Copy only the requirements file first to leverage Docker's layer caching. # This layer will only be re-run if requirements.txt changes. COPY requirements.txt . # Install Python dependencies from the requirements file. # --no-cache-dir reduces image size. RUN pip install --no-cache-dir -r requirements.txt # --- Application Code --- # Now, copy the rest of the application code into the container. # This layer will be rebuilt on code changes, but the dependencies above will not. COPY . . # --- Port and Command --- # Expose the port the application will run on EXPOSE 7860 # The command to run the application # Using a list format is the recommended way to write CMD CMD ["uvicorn", "predict:app", "--host", "0.0.0.0", "--port", "7860"]