0

系统

  • Docker version 20.10.8, build 3967b7d
  • 带有 Docker 桌面的 Windows 10 专业版

作为一项要求,我必须移植我的 Python3.x 应用程序以支持在arm/v7架构硬件上工作。我有可以为平台/架构构建linux/arm64的GitHub 工作流。linux/amd64其中一个依赖项是numpy,在构建阶段会导致构建时间超过 30 分钟。

它的轮子创建阶段似乎没有移动。为了避免构建的复杂性,我避免使用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/v/7 -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...

在这种跨平台构建期间是否需要设置/配置某些优化,以便减少numpy scipypandas减少轮子创建的构建时间?

4

1 回答 1

0

我面临着同样的问题。不同之处在于opencv花费了太多时间来构建并且获得了相同的输出Building wheel for opencv (PEP 517): still running...

更改 Dockerfile 中的 python 映像版本解决了我的问题。尝试一次python:3.7-buster它不会花费那么长时间来构建 numpy。

于 2021-11-10T19:24:46.097 回答