如何缓存 docker 构建的包管理器下载? [英] How to cache package manager downloads for docker builds?

查看:26
本文介绍了如何缓存 docker 构建的包管理器下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从我的主机运行 composer install,我会点击我的本地作曲家缓存:

If I run composer install from my host, I hit my local composer cache:

  - Installing deft/iso3166-utility (1.0.0)
    Loading from cache

然而,当构建一个包含在其 Dockerfile 中的容器时:

Yet when building a container having in its Dockerfile:

RUN composer install -n -o --no-dev

我下载所有东西,例如:

I download all the things, e.g.:

  - Installing deft/iso3166-utility (1.0.0)
    Downloading: 100%         

这是意料之中的,但我想避免它.即使在重建时,它也会再次下载所有内容.

It's expected, yet I like to avoid it. As even on a rebuilt, it would also download everything again.

我想为作曲家提供一个通用缓存,我也可以将其转发给其他 docker 项目.

I would like to have a universal cache for composer that I could also reshare for other docker projects.

我对此进行了调查,并找到了 定义卷的方法在 Dockerfile 中:

I looked into this and found the approach to define a volume in the Dockerfile:

ENV COMPOSER_HOME=/var/composer
VOLUME /var/composer

我将它添加到我的 Dockerfile 中,并希望只下载一次文件,然后访问缓存.

I added that to my Dockerfile, and expected to only download the files once, and hit the cache afterwards.

当我修改我的 composer 时,例如删除 -o 标志,然后重新运行 docker build .,我希望在构建时命中缓存,但我仍然再次下载供应商.

Yet when I modify my composer, e.g. remove the -o flag, and rerun docker build ., I expected to hit the cache on build, yet I still download the vendors again.

卷应该如何工作以在 docker 容器中拥有数据缓存?

How are volumes supposed to work to have a data cache inside a docker container?

推荐答案

使用实验功能:Docker buildkit(从 docker 18.09 开始支持,docker-compose 1.25.4)

Use the experimental feature : Docker buildkit (Supported Since docker 18.09, docker-compose 1.25.4)

在你的 dockerfile 中

In your dockerfile

# syntax=docker/dockerfile:experimental
FROM ....
# ......  
RUN --mount=type=cache,target=/var/composer composer install -n -o --no-dev

现在在构建之前,请确保已导出 env var:

Now before building, make sure the env var is exported:

export DOCKER_BUILDKIT=1
docker build ....

如果您使用的是 docker-compose,请确保同时导出 COMPOSE_DOCKER_CLI_BUILD:

If you are using docker-compose, make sure to export also COMPOSE_DOCKER_CLI_BUILD :

export COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1
docker-compose build ...

如果它不适用于 docker-compose,请确保您的 docker-compose 版本高于 1.25.4

If it does not work with docker-compose, make sure your docker-compose version is above 1.25.4

docker-compose version

这篇关于如何缓存 docker 构建的包管理器下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆