Pinning opencv-python-headless doesn't help with ultralytics: pip still installs opencv-python (needs libGL.so.1 / libgthread in slim images), and both packages share one cv2
Fix: in python:*-slim images, either install the GUI-OpenCV system libraries (apt-get install -y --no-install-recommends libgl1 libglib2.0-0), or remove the GUI wheel after installing ultralytics and force-reinstall headless:
RUN pip install --no-cache-dir ultralytics==8.3.50 opencv-python-headless \
&& pip uninstall -y opencv-python \
&& pip install --no-cache-dir --force-reinstall --no-deps opencv-python-headlessSymptom: a requirements file with ultralytics==8.3.50 and opencv-python-headless==4.10.0.84, written specifically 'to avoid X11 dependencies'. The build log still shows:
Collecting opencv-python>=4.6.0 (from ultralytics==8.3.50->-r requirements.v2.txt (line 1))
Downloading opencv_python-5.0.0.93-...manylinux_2_28_x86_64.whl (73.8 MB)
Installing collected packages: ..., opencv-python-headless, opencv-python, ...ultralytics declares opencv-python as a hard dependency, and pip treats the headless wheel as a different package. Both install into the same cv2/ directory, and whichever is unpacked last wins. Here that's the GUI build, which needs libGL.so.1 (ImportError: libGL.so.1: cannot open shared object file), plus a 74 MB download. If you later uninstall either package, cv2 itself gets deleted and imports fail.
On Debian trixie-based images, apt-get install libgl1 libglib2.0-0 libgomp1 libgthread-2.0-0 still resolves (it pulls libglib2.0-0t64 and ~41 packages, 218 MB). Version pins also differed: opencv-python resolved to 5.0.0.93 while the headless pin was 4.10.
Verify: pip list | grep -i opencv should show one OpenCV distribution, and python -c "import cv2; print(cv2.__version__)" should run in the final image.