python-detector-worker/Dockerfile.base
ziesorx a45f76884f
Some checks failed
Build Worker Base and Application Images / check-base-changes (push) Successful in 7s
Build Worker Base and Application Images / build-base (push) Failing after 6m4s
Build Worker Base and Application Images / build-docker (push) Has been skipped
Build Worker Base and Application Images / deploy-stack (push) Has been skipped
fix: make ffmpeg support
2025-09-25 23:23:56 +07:00

198 lines
No EOL
7.8 KiB
Text

# Base image with complete ML and hardware acceleration stack
FROM pytorch/pytorch:2.8.0-cuda12.6-cudnn9-runtime
# Install build dependencies and system libraries
RUN apt-get update && apt-get install -y \
# Build tools
build-essential \
cmake \
git \
pkg-config \
wget \
unzip \
yasm \
nasm \
# System libraries
libgl1-mesa-glx \
libglib2.0-0 \
libgomp1 \
# Core media libraries (essential ones only)
libjpeg-dev \
libpng-dev \
libx264-dev \
libx265-dev \
libvpx-dev \
libmp3lame-dev \
libv4l-dev \
# TurboJPEG for fast JPEG encoding
libturbojpeg0-dev \
# Python development
python3-dev \
python3-numpy \
&& rm -rf /var/lib/apt/lists/*
# Install CUDA development tools (required for FFmpeg CUDA compilation)
RUN apt-get update && apt-get install -y \
cuda-nvcc-12-6 \
libcuda1 \
cuda-cudart-dev-12-6 \
cuda-driver-dev-12-6 \
|| echo "CUDA development packages not available, continuing without them" && \
rm -rf /var/lib/apt/lists/*
# Try to install NVIDIA packages (may not be available in all environments)
RUN apt-get update && apt-get install -y \
libnvidia-encode-535 \
libnvidia-decode-535 \
|| echo "NVIDIA packages not available, continuing without them" && \
rm -rf /var/lib/apt/lists/*
# Install NVIDIA Video Codec SDK headers
RUN cd /tmp && \
wget https://github.com/FFmpeg/nv-codec-headers/archive/refs/tags/n12.1.14.0.zip && \
unzip n12.1.14.0.zip && \
cd nv-codec-headers-n12.1.14.0 && \
make install && \
rm -rf /tmp/*
# Build FFmpeg from source with NVIDIA CUVID support
ENV FFMPEG_VERSION=6.0
# Ensure CUDA paths are available for FFmpeg compilation
ENV PATH="/usr/local/cuda/bin:${PATH}"
ENV LD_LIBRARY_PATH="/usr/local/cuda/lib64:${LD_LIBRARY_PATH}"
RUN cd /tmp && \
wget https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.xz && \
tar xf ffmpeg-${FFMPEG_VERSION}.tar.xz && \
cd ffmpeg-${FFMPEG_VERSION} && \
# Configure with explicit CUVID support (with fallback)
(./configure \
--enable-gpl \
--enable-nonfree \
--enable-shared \
--enable-libx264 \
--enable-libx265 \
--enable-libvpx \
--enable-libmp3lame \
--enable-cuda-nvcc \
--enable-cuda-llvm \
--enable-cuvid \
--enable-nvdec \
--enable-nvenc \
--enable-libnpp \
--enable-decoder=h264_cuvid \
--enable-decoder=hevc_cuvid \
--enable-decoder=mjpeg_cuvid \
--enable-decoder=mpeg1_cuvid \
--enable-decoder=mpeg2_cuvid \
--enable-decoder=mpeg4_cuvid \
--enable-decoder=vc1_cuvid \
--enable-encoder=h264_nvenc \
--enable-encoder=hevc_nvenc \
--extra-cflags="-I/usr/local/cuda/include" \
--extra-ldflags="-L/usr/local/cuda/lib64" \
--extra-libs="-lcuda -lcudart -lnvcuvid -lnvidia-encode" \
--nvccflags="-gencode arch=compute_60,code=sm_60 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86" \
|| echo "CUDA configuration failed, trying basic configuration..." && \
./configure \
--enable-gpl \
--enable-nonfree \
--enable-shared \
--enable-libx264 \
--enable-libx265 \
--enable-libvpx \
--enable-libmp3lame) \
&& make -j$(nproc) && \
make install && \
ldconfig && \
# Verify CUVID decoders are available
echo "=== Verifying FFmpeg CUVID Support ===" && \
ffmpeg -hide_banner -decoders 2>/dev/null | grep cuvid && \
echo "=== Verifying FFmpeg NVENC Support ===" && \
ffmpeg -hide_banner -encoders 2>/dev/null | grep nvenc && \
cd / && rm -rf /tmp/*
# Build OpenCV from source with custom FFmpeg and full CUDA support
ENV OPENCV_VERSION=4.8.1
RUN cd /tmp && \
wget -O opencv.zip https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip && \
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip && \
unzip opencv.zip && \
unzip opencv_contrib.zip && \
cd opencv-${OPENCV_VERSION} && \
mkdir build && cd build && \
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D WITH_CUVID=ON \
-D BUILD_opencv_cudacodec=ON \
-D WITH_FFMPEG=ON \
-D WITH_LIBV4L=ON \
-D BUILD_opencv_python3=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib-${OPENCV_VERSION}/modules \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D PYTHON_INCLUDE_DIR=$(python3 -c "import sysconfig; print(sysconfig.get_path('include'))") \
-D PYTHON_LIBRARY=$(python3 -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
.. && \
make -j$(nproc) && \
make install && \
ldconfig && \
cd / && rm -rf /tmp/*
# Set environment variables for maximum hardware acceleration
ENV LD_LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/lib:${LD_LIBRARY_PATH}"
ENV PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH}"
ENV PYTHONPATH="/usr/local/lib/python3.10/dist-packages:${PYTHONPATH}"
# Optimized environment variables for hardware acceleration
ENV OPENCV_FFMPEG_CAPTURE_OPTIONS="rtsp_transport;tcp|hwaccel;cuda|hwaccel_device;0|video_codec;h264_cuvid|hwaccel_output_format;cuda"
ENV OPENCV_FFMPEG_WRITER_OPTIONS="video_codec;h264_nvenc|preset;fast|tune;zerolatency|gpu;0"
ENV CUDA_VISIBLE_DEVICES=0
ENV NVIDIA_VISIBLE_DEVICES=all
ENV NVIDIA_DRIVER_CAPABILITIES=compute,video,utility
# Copy and install base requirements (exclude opencv-python since we built from source)
COPY requirements.base.txt .
RUN grep -v opencv-python requirements.base.txt > requirements.tmp && \
mv requirements.tmp requirements.base.txt && \
pip install --no-cache-dir -r requirements.base.txt
# Verify complete hardware acceleration setup
RUN echo "=== Hardware Acceleration Verification ===" && \
echo "FFmpeg Hardware Accelerators:" && \
(ffmpeg -hide_banner -hwaccels 2>/dev/null || echo "FFmpeg hwaccels command failed") && \
echo "" && \
echo "FFmpeg CUVID Decoders (NVIDIA):" && \
(ffmpeg -hide_banner -decoders 2>/dev/null | grep -E "cuvid" || echo "No CUVID decoders found") && \
echo "" && \
echo "FFmpeg NVENC Encoders (NVIDIA):" && \
(ffmpeg -hide_banner -encoders 2>/dev/null | grep -E "nvenc" || echo "No NVENC encoders found") && \
echo "" && \
echo "Testing CUVID decoder compilation (no GPU required):" && \
(ffmpeg -hide_banner -f lavfi -i testsrc=duration=0.1:size=64x64:rate=1 -c:v libx264 -f null - 2>/dev/null && echo "✅ FFmpeg basic functionality working" || echo "❌ FFmpeg basic test failed") && \
echo "" && \
echo "OpenCV Configuration:" && \
(python3 -c "import cv2; print('OpenCV version:', cv2.__version__); build_info = cv2.getBuildInformation(); print('CUDA support:', 'CUDA' in build_info); print('CUVID support:', 'CUVID' in build_info); print('FFmpeg support:', 'FFMPEG' in build_info)" || echo "OpenCV verification failed") && \
echo "" && \
echo "=== Verification Complete (build-time only) ==="
# Set working directory
WORKDIR /app
# Create images directory for bind mount
RUN mkdir -p /app/images && \
chmod 755 /app/images
# This base image will be reused for all worker builds
CMD ["python3", "-m", "fastapi", "run", "--host", "0.0.0.0", "--port", "8000"]