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

Docker buildx跨arm/v7架构编译numpy等库耗时过长如何优化?

系统环境

  • Docker version 20.10.8, build 3967b7d
  • 搭载Docker Desktop的Windows 10 Pro系统

业务要求将Python3.x应用移植到适配arm/v7架构的硬件运行,已有的GitHub Workflows可正常构建linux/arm64和linux/amd64平台/架构的镜像。应用依赖项中的numpy在构建阶段导致构建时长超过30分钟,wheel创建阶段几乎没有进度。为了避免构建复杂度提升,没有使用alpine基础镜像,而是选择slim系列镜像,通过多阶段docker构建安装所需依赖。

所用Dockerfile内容如下:

FROM python:3.7-slim AS compile-image

# This prevents Python from writing out pyc files
ENV PYTHONDONTWRITEBYTECODE 1
# This keeps Python from buffering stdin/stdout
ENV PYTHONUNBUFFERED 1

RUN apt-get update
RUN apt-get install -y --no-install-recommends build-essential gcc

RUN python -m venv /opt/venv
# Make sure we use the virtualenv:
ENV PATH="/opt/venv/bin:$PATH"

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY setup.py .
COPY . .
RUN pip install .

FROM python:3.7-slim AS build-image
COPY --from=compile-image /opt/venv /opt/venv
COPY scripts/docker-entrypoint.sh /entrypoint.sh
# Make sure we use the virtualenv:
ENV PATH="/opt/venv/bin:$PATH"

RUN chmod +x /entrypoint.sh

ENTRYPOINT [ "/entrypoint.sh" ]

CMD ["app", "-c", "config.yaml"]

构建输出

构建命令:

docker buildx build --platform linux/arm/v7 -t myDockerAcc/pyapp .

构建日志:

[+] Building 162.2s (8/17)
[+] Building 1554.2s (10/17)
 => [internal] load build definition from Dockerfile                                                                                                                                                        0.1s
 => => transferring dockerfile: 1.67kB                                                                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                                                                           0.1s
 => => transferring context: 2B                                                                                                                                                                             0.0s
 => [internal] load metadata for docker.io/library/python:3.7-slim                                                                                                                                          2.2s
 => [auth] library/python:pull token for registry-1.docker.io                                                                                                                                               0.0s
 => CACHED [build-image 1/4] FROM docker.io/library/python:3.7-slim@sha256:c2cc09c3de140f59b3065b9518fa7beb5fbedb4414762963bfe01079ce219f2e                                                                 0.0s
 => => resolve docker.io/library/python:3.7-slim@sha256:c2cc09c3de140f59b3065b9518fa7beb5fbedb4414762963bfe01079ce219f2e                                                                                    0.0s
 => [internal] load build context                                                                                                                                                                           0.7s
 => => transferring context: 4.77kB                                                                                                                                                                         0.7s
 => [compile-image 2/9] RUN apt-get update                                                                                                                                                                 31.8s
 => [compile-image 3/9] RUN apt-get install -y --no-install-recommends build-essential gcc                                                                                                                102.7s
 => [compile-image 4/9] RUN python -m venv /opt/venv                                                                                                                                                       55.8s
 => [compile-image 5/9] COPY requirements.txt .                                                                                                                                                             0.3s
 => [compile-image 6/9] RUN pip install --no-cache-dir -r requirements.txt                                                                                                                               1361.0s
 => => #   Building wheel for numpy (PEP 517): started
 => => #   Building wheel for numpy (PEP 517): still running...
 => => #   Building wheel for numpy (PEP 517): still running...

优化方案

  • 优先使用预编译wheel包:首先升级镜像内的pip、setuptools、wheel工具到最新版本,更换为针对ARM架构适配的Python软件源,这类源提前编译好了numpy、scipy、pandas等常见库的armv7版本wheel包,安装时直接拉取即可跳过源码编译步骤,耗时可以从几十分钟压缩到几十秒。
  • 优化构建缓存配置:给Docker Buildx开启层缓存,拆分依赖安装步骤,先拷贝仅包含固定基础依赖(比如numpy这类不常更新的科学计算库)的requirements文件单独安装,再拷贝其他业务依赖安装,也可以给RUN指令添加缓存挂载参数,把pip、apt的缓存目录挂载到本地缓存,避免每次构建重复下载依赖、重复编译。
  • 修正构建参数:原构建命令中的平台参数书写错误,linux/arm/v/7多了一个斜杠,正确写法为linux/arm/v7,参数错误会导致无法匹配到对应架构的预编译包,只能回退到源码编译。
  • 更换预装依赖的基础镜像:可以直接选用已经预装好numpy、scipy等科学计算库的armv7版本Python基础镜像,省去自己编译的步骤。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 16:36:04