Skip to content

Commit b8da8b6

Browse files
committed
Support reproducible builds (except packages)
See docker-library/official-images issue 16044 - `SOURCE_DATE_EPOCH` is added. The value is consumed by the build scripts to make the binary reproducible. - `/tmp/*` is removed as they contain files created by `memcached` - For Debian, `/var/log/*` is removed as they contain timestamps - For Debian, `/var/cache/ldconfig/aux-cache` is removed as they contain inode numbers, etc. - For Alpine, virtual package versions are pinned to "0" to eliminate the timestamp-based version numbers that appear in `/etc/apk/world` and `/lib/apk/db/installed` > [!NOTE] > The following topics are NOT covered by this commit: > > - To reproduce file timestamps in layers, BuildKit has to be executed with > `--output type=<TYPE>,rewrite-timestamp=true`. > Needs BuildKit v0.13 or later. > > - To reproduce the base image by the hash, reproducers may: > - modify the `FROM` instruction in Dockerfile manually > - or, use the `CONVERT` action of source policies to replace the base image. > <https://github.com/moby/buildkit/blob/v0.13.2/docs/build-repro.md> > > - To reproduce packages, see the `RUN` instruction hook proposed in > moby/buildkit issue 4576 Signed-off-by: Akihiro Suda <[email protected]>
1 parent 3598423 commit b8da8b6

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

1/alpine/Dockerfile

+14-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

1/debian/Dockerfile

+19-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile.template

+21-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ FROM alpine:{{ .alpine.version }}
44
FROM debian:{{ .debian.version }}-slim
55
{{ ) end -}}
66

7+
# The global SOURCE_DATE_EPOCH is consumed by commands that are not associated with a source artifact.
8+
# This is not propagated from --build-arg: https://github.com/moby/buildkit/issues/4576#issuecomment-2159501282
9+
ENV SOURCE_DATE_EPOCH 0
10+
711
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
12+
# On Debian, useradd recognizes SOURCE_DATE_EPOCH to reproduce the "lastchanged" field in /etc/shadow.
813
{{ if env.variant == "alpine" then ( -}}
914
RUN set -eux; \
1015
addgroup -g 11211 memcache; \
@@ -24,7 +29,9 @@ RUN set -eux; \
2429
apt-get install -y --no-install-recommends \
2530
libsasl2-modules \
2631
; \
27-
rm -rf /var/lib/apt/lists/*
32+
rm -rf /var/lib/apt/lists/*; \
33+
# clean up for reproducibility
34+
rm -rf /var/log/* /var/cache/ldconfig/aux-cache
2835
{{ ) end -}}
2936

3037
ENV MEMCACHED_VERSION {{ .version }}
@@ -34,7 +41,7 @@ ENV MEMCACHED_SHA1 {{ .sha1 }}
3441
RUN set -eux; \
3542
\
3643
{{ if env.variant == "alpine" then ( -}}
37-
apk add --no-cache --virtual .build-deps \
44+
apk add --no-cache --virtual .build-deps=0 \
3845
ca-certificates \
3946
coreutils \
4047
cyrus-sasl-dev \
@@ -67,6 +74,8 @@ RUN set -eux; \
6774
wget \
6875
; \
6976
rm -rf /var/lib/apt/lists/*; \
77+
# clean up for reproducibility
78+
rm -rf /var/log/* /var/cache/ldconfig/aux-cache; \
7079
{{ ) end -}}
7180
\
7281
wget -O memcached.tar.gz "$MEMCACHED_URL"; \
@@ -78,6 +87,10 @@ RUN set -eux; \
7887
cd /usr/src/memcached; \
7988
\
8089
gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
90+
SOURCE_DATE_EPOCH="$(find . -type f -exec stat -c '%Y' {} + | sort -nr | head -n1)"; \
91+
export SOURCE_DATE_EPOCH; \
92+
# for logging validation/edification
93+
date --date "@$SOURCE_DATE_EPOCH" --rfc-2822; \
8194
./configure \
8295
--build="$gnuArch" \
8396
--enable-extstore \
@@ -109,7 +122,7 @@ RUN set -eux; \
109122
| sort -u \
110123
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
111124
)"; \
112-
apk add --no-network --virtual .memcached-rundeps $runDeps; \
125+
apk add --no-network --virtual .memcached-rundeps=0 $runDeps; \
113126
apk del --no-network .build-deps; \
114127
{{ ) else ( -}}
115128
apt-mark auto '.*' > /dev/null; \
@@ -123,9 +136,13 @@ RUN set -eux; \
123136
| xargs -r apt-mark manual \
124137
; \
125138
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
139+
# clean up for reproducibility
140+
rm -rf /var/log/* /var/cache/ldconfig/aux-cache; \
126141
{{ ) end -}}
127142
\
128-
memcached -V
143+
memcached -V ;\
144+
# clean up for reproducibility
145+
rm -rf /tmp/*
129146

130147
COPY docker-entrypoint.sh /usr/local/bin/
131148
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat

0 commit comments

Comments
 (0)