If I run composer install
from my host, I hit my local composer cache:
- Installing deft/iso3166-utility (1.0.0)
Loading from cache
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.
I would like to have a universal cache for composer that I could also reshare for other docker projects.
I looked into this and found the approach to define a volume in the Dockerfile:
ENV COMPOSER_HOME=/var/composer
VOLUME /var/composer
I added that to my Dockerfile
, and expected to only download the files once, and hit the cache afterwards.
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.
How are volumes supposed to work to have a data cache inside a docker container?
Anyone looking for this from now should be able to use
This general principle seems to work for most package installers, you just need to find the right cache dir (
npm
uses$HOME/.npm
, etc.), and it will obviously change if you are not building asroot
.