You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker构建时pip安装optimum[onnxruntime]依赖失败求助

问题原因
  • Alpine镜像默认缺少编译Python扩展所需的系统依赖(如gcc、musl-dev),安装optimum[onnxruntime]时部分依赖需要编译,导致构建流程中断。
  • pip自动拉取了optimum的旧版本(0.1.x),这些版本的源码包存在文件结构缺陷,构建wheel时无法找到optimum/version.py文件。
解决方案

1. 添加系统编译依赖

Alpine镜像需要先安装编译工具链,才能构建需要编译的Python包,在RUN pip install --upgrade pip之前添加:

RUN apk add --no-cache gcc musl-dev linux-headers

2. 指定optimum的稳定新版本

避免使用存在问题的旧版本,直接指定一个稳定的新版本(比如>=1.10.0),修改pip安装命令:

RUN pip install flask transformers "optimum[onnxruntime]>=1.10.0" gunicorn

3. 修正gunicorn绑定地址

原配置绑定127.0.0.1只能在容器内部访问,改成0.0.0.0才能让外部正常访问:

ENTRYPOINT ["gunicorn", "--bind=0.0.0.0:5000", "app:app"]

4. 推荐使用requirements.txt管理依赖(可选但更规范)

创建requirements.txt文件:

flask
transformers
optimum[onnxruntime]>=1.10.0
gunicorn

然后修改Dockerfile中的依赖安装步骤:

COPY requirements.txt .
RUN pip install --upgrade pip && pip install -r requirements.txt
修改后的完整Dockerfile
# 使用Python 3.8 Alpine基础镜像
FROM python:3.8-alpine3.15

# 设置工作目录
WORKDIR /app

# 安装系统编译依赖
RUN apk add --no-cache gcc musl-dev linux-headers

# 升级pip并安装Python依赖
RUN pip install --upgrade pip
RUN pip install flask transformers "optimum[onnxruntime]>=1.10.0" gunicorn

# 复制应用代码和模型
COPY ["./models", "app.py", "./"]

# 暴露端口
EXPOSE 5000

# 启动应用
ENTRYPOINT ["gunicorn", "--bind=0.0.0.0:5000", "app:app"]

内容的提问来源于stack exchange,提问作者Amit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:21:33