The common wisdom, recommended even by a popular hadolint, is to always run package installs without caches and/or drop them.
However, reading about --mount=type=cache
I get a sense that a following method may make more sense:
RUN --mount=type=cache,target=/var/log \
--mount=type=cache,target=/var/cache,sharing=locked \
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked <<EOT
set -eux
. /etc/os-release
if [ "$ID" = "alpine" ]; then
apk update
apk add <packages>
elif [ "$ID" = "debian" ]; then
rm -f /etc/apt/apt.conf.d/docker-clean
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
apt-get update
apt-get install -y --no-install-recommends <packages>
fi
EOT
Even the official documentation in âcaching apt packagesâ example suggests such approach. However, I am unable to find pros/cons for both approaches, beyond the caching approach being very verbose with mounts.