Skip to content

Commit 4e788b5

Browse files
committed
Dockerfile: Cache the apt cache locally
This will accelerate rebuilds, specifically when switching to snapshot.debian.org which is known to be slow. As /var/cache/apt is now managed outside of the container, we can and want to drop the calls of apt-get clean and the removal of /var/lib/apt/lists. As sharing=locked seemed to cause issues with buildah, make the mode configurable for unofficial non-concurrent local builds via build arg CACHE_SHARING=shared. Signed-off-by: Jan Kiszka <[email protected]>
1 parent 0ce35db commit 4e788b5

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Dockerfile

+17-9
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ ARG DEBIAN_TAG=bookworm-slim
2626
FROM debian:${DEBIAN_TAG} as kas-base
2727

2828
ARG SOURCE_DATE_EPOCH
29+
ARG CACHE_SHARING=locked
2930

3031
ARG DEBIAN_TAG=bookworm-slim
3132
ENV DEBIAN_BASE_IMAGE_TAG=${DEBIAN_TAG}
3233

3334
ARG TARGETPLATFORM
3435
ARG DEBIAN_FRONTEND=noninteractive
3536
ENV LANG=en_US.utf8
36-
RUN if echo "${DEBIAN_TAG}" | grep -q "[0-9]"; then \
37+
RUN --mount=type=cache,target=/var/cache/apt,sharing=${CACHE_SHARING} \
38+
--mount=type=cache,target=/var/lib/apt,sharing=${CACHE_SHARING} \
39+
rm -f /etc/apt/apt.conf.d/docker-clean && \
40+
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-packages.conf && \
41+
if echo "${DEBIAN_TAG}" | grep -q "[0-9]"; then \
3742
sed -i -e '/^URIs:/d' -e 's|^# http://snapshot\.|URIs: http://snapshot-cloudflare.|' \
3843
/etc/apt/sources.list.d/debian.sources; \
3944
echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/use-snapshot.conf; \
@@ -47,8 +52,7 @@ RUN if echo "${DEBIAN_TAG}" | grep -q "[0-9]"; then \
4752
gosu lsb-release file vim less procps tree tar bzip2 zstd pigz lz4 unzip tmux libncurses-dev \
4853
git-lfs mercurial iproute2 ssh-client telnet curl rsync gnupg awscli sudo \
4954
socat bash-completion python3-shtab python3-git && \
50-
apt-get clean && \
51-
rm -rf /var/lib/apt/* /var/log/* /tmp/* /var/tmp/* /var/cache/ldconfig/aux-cache && \
55+
rm -rf /var/log/* /tmp/* /var/tmp/* /var/cache/ldconfig/aux-cache && \
5256
rm -f /etc/gitconfig && \
5357
git config --system filter.lfs.clean 'git-lfs clean -- %f' && \
5458
git config --system filter.lfs.smudge 'git-lfs smudge -- %f' && \
@@ -92,12 +96,15 @@ ENTRYPOINT ["/container-entrypoint"]
9296
FROM kas-base as kas-isar
9397

9498
ARG SOURCE_DATE_EPOCH
99+
ARG CACHE_SHARING=locked
95100

96101
# The install package list are actually taking 1:1 from their documentation,
97102
# so there some packages that can already installed by other downstream layers.
98103
# This will not change any image sizes on all the layers in use.
99104
ENV LC_ALL=en_US.UTF-8
100-
RUN apt-get update && \
105+
RUN --mount=type=cache,target=/var/cache/apt,sharing=${CACHE_SHARING} \
106+
--mount=type=cache,target=/var/lib/apt,sharing=${CACHE_SHARING} \
107+
apt-get update && \
101108
apt-get install -y -f --no-install-recommends \
102109
binfmt-support bzip2 mmdebstrap arch-test apt-utils dosfstools \
103110
dpkg-dev gettext-base git mtools parted python3 python3-distutils \
@@ -106,8 +113,7 @@ RUN apt-get update && \
106113
umoci skopeo \
107114
python3-botocore \
108115
debootstrap && \
109-
apt-get clean && \
110-
rm -rf /var/lib/apt/* /var/log/* /tmp/* /var/tmp/* /var/cache/ldconfig/aux-cache && \
116+
rm -rf /var/log/* /tmp/* /var/tmp/* /var/cache/ldconfig/aux-cache && \
111117
sbuild-adduser builder && \
112118
sed -i 's|# kas-isar: ||g' /container-entrypoint
113119

@@ -120,12 +126,15 @@ USER builder
120126
FROM kas-base as kas
121127

122128
ARG SOURCE_DATE_EPOCH
129+
ARG CACHE_SHARING=locked
123130

124131
# The install package list are actually taking 1:1 from their documentation
125132
# (exception: pylint3 -> pylint), so there some packages that can already
126133
# installed by other downstream layers. This will not change any image sizes
127134
# on all the layers in use.
128-
RUN apt-get update && \
135+
RUN --mount=type=cache,target=/var/cache/apt,sharing=${CACHE_SHARING} \
136+
--mount=type=cache,target=/var/lib/apt,sharing=${CACHE_SHARING} \
137+
apt-get update && \
129138
apt-get install --no-install-recommends -y \
130139
gawk wget git diffstat unzip texinfo \
131140
gcc build-essential chrpath socat cpio python3 python3-pip python3-pexpect \
@@ -134,7 +143,6 @@ RUN apt-get update && \
134143
if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
135144
apt-get install --no-install-recommends -y gcc-multilib g++-multilib; \
136145
fi && \
137-
apt-get clean && \
138-
rm -rf /var/lib/apt/* /var/log/* /tmp/* /var/tmp/* /var/cache/ldconfig/aux-cache
146+
rm -rf /var/log/* /tmp/* /var/tmp/* /var/cache/ldconfig/aux-cache
139147

140148
USER builder

0 commit comments

Comments
 (0)