# Use a slim Python image for smaller size FROM python:3.10-slim # --- User Setup for Security and Writable Config Dirs --- # Define build arguments for a non-root username and user ID ARG NB_USER=appuser ARG NB_UID=1000 # Set environment variables for the user and their home directory ENV USER ${NB_USER} ENV HOME /home/${NB_USER} # Create the non-root user, set up their home directory, and grant ownership RUN adduser --disabled-password --gecos "" --uid ${NB_UID} ${NB_USER} \ && mkdir -p ${HOME}/.config \ && chown -R ${NB_USER}:${NB_USER} ${HOME} # Set environment variables for Matplotlib and Ultralytics # These direct them to use a writable directory within the user's home ENV MPLCONFIGDIR=${HOME}/.config/matplotlib ENV YOLO_CONFIG_DIR=${HOME}/.config/ultralytics # Ensure the directories exist and are writable by the non-root user RUN mkdir -p ${MPLCONFIGDIR} \ && mkdir -p ${YOLO_CONFIG_DIR} \ && chown -R ${NB_USER}:${NB_USER} ${MPLCONFIGDIR} \ && chown -R ${NB_USER}:${NB_USER} ${YOLO_CONFIG_DIR} # --- System Dependencies --- # Switch to root to install system-level packages USER root RUN apt-get update && apt-get install -y --no-install-recommends \ libgl1 \ libglib2.0-0 \ curl \ # Clean up apt caches to keep image size small && rm -rf /var/lib/apt/lists/* # --- Application Setup --- # Set the working directory for the application WORKDIR /app # Switch back to the non-root user for installing Python packages and copying application code USER ${NB_USER} # Copy requirements.txt and install Python packages # Ensure Flask is in your requirements.txt (and gunicorn is NOT if you're not using it) COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Copy the rest of your application's source code COPY . . # --- Flask Development Server Configuration --- # Set Flask application entry point ENV FLASK_APP=app.py # Set FLASK_RUN_HOST to listen on all interfaces ENV FLASK_RUN_HOST=0.0.0.0 # Set FLASK_RUN_PORT (optional, defaults to 5000) ENV FLASK_RUN_PORT=5000 # Set FLASK_ENV to 'development' if you want auto-reload and debugger. # For a more "production-like" test without Gunicorn, use 'production' # but be aware of the limitations mentioned previously (not for real production traffic). ENV FLASK_ENV=development # Expose the port Flask will listen on EXPOSE 5000 # Command to run the Flask application directly # Using 'python -m flask run' is the recommended way. CMD ["python", "-m", "flask", "run"] # Alternative CMD if your app.py contains app.run() directly: # CMD ["python", "app.py"]