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

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

问题描述

如果我从主机运行 composer install ,则会命中本地Composer缓存:

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天全站免登陆