A Docker environment for building and running OpenCV 4.12.0 with CUDA/cuDNN GPU acceleration on Ubuntu 22.04.
- OpenCV 4.12.0 built with
opencv_contribmodules - CUDA 12.2 + cuDNN 8 with DNN GPU backend enabled
- Hardware acceleration: OpenGL, GTK, GStreamer, FFmpeg, V4L2, dc1394, OpenEXR
- Dev tools:
cmake,gcc/g++,clang,ninja,git,gdb,ccache,python3 - Non-root
adminuser with passwordless sudo
- Host OS: Linux (tested on Ubuntu 22.04 / 24.04)
- NVIDIA GPU driver: version ≥ 535
- Docker Engine + NVIDIA Container Toolkit → Installation Guide
Check your driver version:
nvidia-smidocker build -t opencv-gpu-build .This will clone and compile OpenCV from source inside the image. It takes a while.
Use the provided script and pass a local directory to mount as your workspace:
./run_dev.sh /path/to/your/projectThis mounts your directory at /workspace inside the container and drops you into a bash shell.
Note: The script also forwards your display (
$DISPLAY) and/dev/video0for GUI windows and webcam access. Make sure X11 forwarding is allowed on the host if needed:xhost +local:docker
Once inside the container, run:
python3 -c "import cv2; print('OpenCV:', cv2.__version__); print('CUDA devices:', cv2.cuda.getCudaEnabledDeviceCount())"You should see your OpenCV version and a non-zero CUDA device count.
The Dockerfile defaults to CUDA architectures 86;89;90 (RTX 30/40 series). If your GPU is different, set the build arg:
docker build --build-arg CMAKE_CUDA_ARCHITECTURES=75 -t opencv-gpu-build .Common values: 75 (RTX 20xx), 86 (RTX 30xx), 89 (RTX 40xx).
The opencv_example/ directory contains a YOLO11 object detection demo using OpenCV's DNN module.
Inside the container:
cd /workspace/opencv_example
mkdir build && cd build
cmake ..
make -j$(nproc)
./mainThe example uses models/yolo11m.onnx (included) and the COCO class list from coco/coco.names.
.
├── Dockerfile # Builds the OpenCV + CUDA image
├── run_dev.sh # Helper script to run the container
├── opencv_example/ # YOLO11 object detection demo
│ ├── main.cpp
│ ├── CMakeLists.txt
│ ├── coco/ # COCO class names and config
│ ├── models/ # ONNX model files (yolo11m.onnx)
│ └── utils/ # Helper scripts (pt_to_onnx, yaml_to_names)
└── opencv_lessons_code/ # Standalone lesson examples
├── lesson_1.cpp
├── lesson_2.cpp
├── histogram_calculation.cpp
├── shi-tomasi.cpp
└── CMakeLists.txt
The opencv_lessons_code/ directory contains standalone examples covering basic OpenCV GPU operations. Build them the same way as the main example:
cd /workspace/opencv_lessons_code
mkdir build && cd build
cmake ..
make -j$(nproc)