gpt4 book ai didi

pip - 使用 github 操作在 docker builder 中缓存 pip 依赖项

转载 作者:行者123 更新时间:2023-12-05 02:30:39 30 4
gpt4 key购买 nike

我正在使用 github 操作来构建 docker 镜像。我缓存了 docker 层(效果很好)但有时层没有被重用(不知道为什么!我正在关注 best practices 但忽略它)并且我正在使用后备 pip 缓存来加速构建。

Dockerfile 非常简单:

# syntax=docker/dockerfile:1.2
FROM python:3.10-slim
RUN python -m pip install --upgrade pip && pip cache dir
COPY requirements.txt ./
RUN --mount=type=cache,target=/root/.cache/pip/ pip install -r requirements.txt

我正在使用 --mount 标志 from syntax 1.2 “允许构建容器为编译器和包管理器缓存目录”

Github 操作缓存(我想挂载到构建容器上):

- name: Cache pip
uses: actions/cache@v3
with:
path: /root/.cache/pip
key: pip-{{ hashFiles('requirements*.txt') }}

和码头 build

- name: Build and push
uses: docker/build-push-action@v2
with:
cache-from: type=gha
cache-to: type=gha,mode=max

如何将 pip 缓存挂载到 docker builder 容器中?

现在在 dockerfile 中使用这个命令缓存是空的:

RUN --mount=type=cache,target=/root/.cache/pip/ pip cache info && ls /root/.cache/pip

输出:

19 RUN --mount=type=cache,target=/root/.cache/pip/ pip cache info && ls /root/.cache/pip
#19 0.387 Package index page cache location: /root/.cache/pip/http
#19 0.387 Package index page cache size: 0 bytes
#19 0.387 Number of HTTP files: 0
#19 0.387 Wheels location: /root/.cache/pip/wheels
#19 0.387 Wheels size: 0 bytes
#19 0.387 Number of wheels: 0
#19 DONE 0.4s

最佳答案

所以这实际上有效,并将我的安装时间从 180 秒减少到 30-40 秒,这有 pip 奇怪,但我让它同时适用于 pip 和 yarn ( yarn 不会随时保存,因为我使用的是 yarn 1 和大部分时间花在I/O读写上)

您需要 buildx v0.8.0+ 来支持 --build-context你需要 docker 前端语法 v1.4+ 来支持 RUN --mount我也用 github actions cache处理安装和缓存 pip 依赖项。看起来像这样

  1. github 缓存操作,安装 pip 要求,还获取 pip 缓存目录:::set-output name=pip-dir::$(pip cache dir) 用于下一步
  2. 我正在使用 docker build and push action , 支持--build-context:
    构建上下文:pip_cache=${{steps.vars.outputs.pip-dir}}
  3. 更新 docker 文件以使用 pip_cache 构建上下文运行 --mount=type=cache,target=/root/.cache/pip,from=pip_cache pip install -r requirements.txt

使用缓存包构建 pip 安装:

#22 RUN --mount=type=cache,target=/root/.cache/pip,from=pip_cache pip install -r requirements.txt
#22 0.587 Requirement already satisfied: wheel in /usr/lib/python3/dist-packages (0.34.2)
#22 0.592 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#22 1.440 Collecting aenum==2.2.3
#22 1.442 Using cached aenum-2.2.3-py3-none-any.whl (40 kB)
#22 1.495 Collecting alembic==1.7.3
#22 1.498 Using cached alembic-1.7.3-py3-none-any.whl (208 kB)

这主要是因为节省了构建轮子的时间,但无论如何下载都非常快。

关于pip - 使用 github 操作在 docker builder 中缓存 pip 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71799975/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com