Alpine镜像安装onnxruntime==1.14.1失败,求解决方案
问题:安装onnxruntime==1.14.1时提示找不到匹配版本
我查阅了onnxruntime官方文档,确认它支持Python 3.6-3.9,于是将Docker基础镜像改为python:3.8.16-alpine3.17,但执行依赖安装时仍出现如下报错:
Collecting numba==0.56.4 Downloading numba-0.56.4.tar.gz (2.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.4/2.4 MB 86.3 MB/s eta 0:00:00 Preparing metadata (setup.py): started Preparing metadata (setup.py): still running... Preparing metadata (setup.py): finished with status 'done' WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/numpy/ Collecting numpy==1.24.2 Downloading numpy-1.24.2.tar.gz (10.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.9/10.9 MB 87.2 MB/s eta 0:00:00 Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Preparing metadata (pyproject.toml): started Preparing metadata (pyproject.toml): finished with status 'done' ERROR: Could not find a version that satisfies the requirement onnxruntime==1.14.1 (from versions: none) ERROR: No matching distribution found for onnxruntime==1.14.1 exit status 1 The command '/bin/sh -c pip3 install -r /root/chat-server/requirements.txt' returned a non-zero code: 1 Error: exit status 1 Usage: github-actions build-push [flags] Flags: -h, --help help for build-push
我的Dockerfile:
FROM python:3.8.16-alpine3.17 LABEL jiangxiaoqiang (xxxx@gmail.com) ENV LANG=en_US.UTF-8 \ LC_ALL=en_US.UTF-8 \ TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \ && echo $TZ > /etc/timezone \ && mkdir -p /root/chat-server # upgrade pip RUN pip install --upgrade pip && apk add --no-cache musl-dev gcc g++ libc-dev linux-headers curl ADD . /root/chat-server/ EXPOSE 8001 RUN pip3 install -r /root/chat-server/requirements.txt # RUN pip3 install -r /root/chat-server/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ WORKDIR /root/chat-server/ ENTRYPOINT exec python3 -m uvicorn main:app --port 8001 --host 0.0.0.0 --reload
依赖清单(requirements.txt):
fastapi==0.95.0 openai==0.27.0 sse-starlette==1.3.3 SQLAlchemy~=1.4.6 pydantic~=1.10.5 rembg==2.0.32 Pillow==9.5.0 aiohttp==3.8.4 aiosignal==1.3.1 anyio==3.6.2 async-timeout==4.0.2 asyncer==0.0.2 attrs==22.2.0 certifi==2022.12.7 charset-normalizer==3.1.0 click==8.1.3 coloredlogs==15.0.1 filetype==1.2.0 flatbuffers==23.3.3 frozenlist==1.3.3 h11==0.14.0 httptools==0.5.0 humanfriendly==10.0 idna==3.4 ImageHash==4.3.1 imageio==2.27.0 lazy_loader==0.2 llvmlite==0.39.1 mpmath==1.3.0 multidict==6.0.4 networkx==3.1 numba==0.56.4 numpy==1.24.2 onnxruntime==1.14.1 opencv-python-headless==4.7.0.72 packaging==23.0 Pillow==9.5.0 platformdirs==3.2.0 pooch==1.7.0 protobuf==4.22.1 pydantic==1.10.7 PyMatting==1.1.8 python-dotenv==1.0.0 python-multipart==0.0.6 PyWavelets==1.4.1 PyYAML==6.0 rembg==2.0.32 requests==2.28.2 scikit-image==0.20.0 scipy==1.10.1 sniffio==1.3.0 starlette==0.26.1 sympy==1.11.1 tifffile==2023.3.21 tqdm==4.65.0 typing_extensions==4.5.0 urllib3==1.26.15 uvicorn==0.21.1 uvloop==0.17.0 watchdog==3.0.0 watchfiles==0.19.0 websockets==11.0 yarl==1.8.2 psycopg2-binary==2.9.6
原因分析
核心问题是Alpine Linux使用的musl C库与标准glibc不兼容:
onnxruntime官方发布的onnxruntime包是基于glibc编译的预编译wheel,PyPI上没有提供适配musl架构的版本,因此pip无法找到匹配的安装包,直接报错。
解决方法
方法一:切换为glibc-based的Python镜像(推荐)
将Dockerfile的基础镜像替换为Debian/Ubuntu系的slim镜像,这类镜像使用glibc,可直接安装官方预编译的onnxruntime包:
FROM python:3.8.16-slim
后续安装依赖的步骤无需修改,能正常拉取onnxruntime==1.14.1。
方法二:使用musl专属的onnxruntime包
onnxruntime提供了针对Alpine/musl环境的专属包onnxruntime-musl,修改requirements.txt中的依赖项:
将onnxruntime==1.14.1替换为:
onnxruntime-musl==1.14.1
修改后pip能找到适配musl架构的版本并完成安装。
方法三:源码编译onnxruntime(不推荐)
该方式耗时较长,需要额外安装cmake、git等编译依赖,仅适合特殊场景。具体步骤包括:
- 在Dockerfile中追加安装编译工具:
apk add --no-cache cmake git - 通过pip从源码安装:
pip install onnxruntime==1.14.1 --no-binary :all:
额外优化建议
- 利用Docker缓存:先拷贝
requirements.txt再执行安装,避免每次代码变更都重新安装所有依赖:ADD requirements.txt /root/chat-server/ RUN pip3 install -r /root/chat-server/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ ADD . /root/chat-server/ - 启用国内PyPI镜像:取消Dockerfile中阿里云镜像源的注释,解决下载超时问题,加速依赖安装。
内容的提问来源于stack exchange,提问作者spark
相关产品推荐
相关产品推荐

