Drive-Safe-Assistant / Dockerfile
themehmi's picture
Update Dockerfile
f84e081 verified
# Dockerfile for Safe Driving Assistant
# This container sets up a fully functional, headless-capable runtime environment
# for the AI Safe Driving Assistant. It includes all necessary C/C++ build
# dependencies for compiling dlib, PortAudio bindings (PyAudio), and sound/video
# codecs, along with Xvfb (Virtual Framebuffer) to prevent OpenCV GUI crashes.
# Use official lightweight Python base image
FROM python:3.11-slim
# Set environment variables to optimize Python performance and docker execution
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
DEBIAN_FRONTEND=noninteractive \
PULSE_SERVER=unix:/tmp/pulseaudio.socket \
FLASK_HOST=0.0.0.0 \
SDL_AUDIODRIVER=dummy
# Set the working directory inside the container
WORKDIR /app
# Install system dependencies (build tools, audio, video, espeak, git, Xvfb)
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
cmake \
g++ \
git \
gfortran \
pkg-config \
libopenblas-dev \
liblapack-dev \
libgl1 \
libglib2.0-0 \
libsm6 \
libxext6 \
libxrender-dev \
libx11-dev \
portaudio19-dev \
libasound2-dev \
alsa-utils \
pulseaudio \
libsdl2-dev \
libsdl2-image-dev \
libsdl2-mixer-dev \
libsdl2-ttf-dev \
libsndfile1 \
espeak \
libespeak-dev \
xvfb \
x11-apps \
&& rm -rf /var/lib/apt/lists/*
# Upgrade pip and wheel, but pin setuptools to prevent breaking face_recognition_models
RUN pip install --no-cache-dir --upgrade pip wheel && \
pip install --no-cache-dir "setuptools<80.0.0"
# Install custom face recognition repositories
RUN pip install "face_recognition @ git+https://github.com/lovnishverma/face_recognition.git"
RUN pip install git+https://github.com/ageitgey/face_recognition_models
# Copy requirements.txt first to leverage Docker's build cache
COPY requirements.txt .
# Install dependencies (dlib, pyaudio, and scipy compilation can take a while)
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application files
COPY . .
# Expose Flask web HUD port (5000 by default in main.py)
EXPOSE 5000
# Create an entrypoint script to automatically spin up Xvfb and start the application
RUN echo '#!/bin/bash\n\
if [ -z "$DISPLAY" ]; then\n\
echo "[Docker-Entrypoint] No DISPLAY detected. Starting Virtual Framebuffer (Xvfb)..."\n\
Xvfb :99 -screen 0 640x480x24 &\n\
export DISPLAY=:99\n\
sleep 1\n\
fi\n\
echo "[Docker-Entrypoint] Starting Safe Driving Assistant..."\n\
exec python main.py\n\
' > /app/entrypoint.sh && chmod +x /app/entrypoint.sh
# Use the virtual display entrypoint script
ENTRYPOINT ["/app/entrypoint.sh"]