Docker中基于php:7.1镜像安装opencv-python时出现cmake编译错误
Docker中基于php:7.1安装opencv-python失败,php:8.0可正常运行
问题复现
使用以下Dockerfile构建镜像时,基于php:7.1会报错,切换为php:8.0则正常执行:
FROM php:7.1 RUN apt-get update RUN apt-get install -y python3-pip RUN pip3 install opencv-python
错误信息
构建过程中触发cmake安装失败的错误,核心内容如下:
Running setup.py bdist_wheel for cmake: finished with status 'error' Traceback (most recent call last): File "/tmp/pip-build-env-qno2yg1l/lib/python3.7/site-packages/skbuild/setuptools_wrap.py", line 613, in setup cmkr = cmaker.CMaker(cmake_executable) File "/tmp/pip-build-env-qno2yg1l/lib/python3.7/site-packages/skbuild/cmaker.py", line 141, in __init__ self.cmake_version = get_cmake_version(self.cmake_executable) File "/tmp/pip-build-env-qno2yg1l/lib/python3.7/site-packages/skbuild/cmaker.py", line 96, in get_cmake_version "Problem with the CMake installation, aborting build. CMake executable is %s" % cmake_executable ... Problem with the CMake installation, aborting build. CMake executable is cmake ---------------------------------------- Failed building wheel for cmake Running setup.py clean for cmake Failed to build cmake
原因分析
php:7.1镜像基于Debian 9(stretch),自带的python3-pip版本为18.1,版本过低,无法正确处理cmake的二进制包,导致需要编译cmake但环境不满足要求。- 新版本
opencv-python依赖的cmake版本较高,Debian 9的默认软件源无法提供兼容版本,pip编译cmake时也会失败。 php:8.0镜像基于Debian 11(bullseye),系统环境和软件包版本更现代,天然兼容最新opencv-python的安装需求。
解决方案
提供两种可行的修改方案:
方案一:升级pip并预先安装系统级cmake
通过系统包管理器安装cmake,避免pip编译cmake,同时升级pip以适配现代Python包的安装逻辑:
FROM php:7.1 RUN apt-get update && apt-get install -y --no-install-recommends python3-pip cmake RUN pip3 install --upgrade pip RUN pip3 install opencv-python
方案二:指定兼容老环境的opencv-python版本
选择一个支持Python 3.7和Debian 9的旧版本opencv-python,例如4.5.5.62:
FROM php:7.1 RUN apt-get update && apt-get install -y --no-install-recommends python3-pip RUN pip3 install --upgrade pip RUN pip3 install opencv-python==4.5.5.62
内容的提问来源于stack exchange,提问作者neubert
相关产品推荐
相关产品推荐

