Skip to content

Commit 50177a8

Browse files
committed
Support reproducible builds (except packages)
See docker-library/official-images issue 16044 - `ARG SOURCE_DATE_EPOCH` is added. The argument 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 4ff42b3 commit 50177a8

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

1/alpine/Dockerfile

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

1/debian/Dockerfile

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

Dockerfile.template

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ 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+
ARG SOURCE_DATE_EPOCH
9+
710
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
11+
# On Debian, useradd recognizes SOURCE_DATE_EPOCH to reproduce the "lastchanged" field in /etc/shadow.
812
{{ if env.variant == "alpine" then ( -}}
913
RUN set -eux; \
1014
addgroup -g 11211 memcache; \
@@ -24,7 +28,9 @@ RUN set -eux; \
2428
apt-get install -y --no-install-recommends \
2529
libsasl2-modules \
2630
; \
27-
rm -rf /var/lib/apt/lists/*
31+
rm -rf /var/lib/apt/lists/*; \
32+
# clean up for reproducibility
33+
rm -rf /var/log/* /var/cache/ldconfig/aux-cache
2834
{{ ) end -}}
2935

3036
ENV MEMCACHED_VERSION {{ .version }}
@@ -34,7 +40,7 @@ ENV MEMCACHED_SHA1 {{ .sha1 }}
3440
RUN set -eux; \
3541
\
3642
{{ if env.variant == "alpine" then ( -}}
37-
apk add --no-cache --virtual .build-deps \
43+
apk add --no-cache --virtual .build-deps=0 \
3844
ca-certificates \
3945
coreutils \
4046
cyrus-sasl-dev \
@@ -67,6 +73,8 @@ RUN set -eux; \
6773
wget \
6874
; \
6975
rm -rf /var/lib/apt/lists/*; \
76+
# clean up for reproducibility
77+
rm -rf /var/log/* /var/cache/ldconfig/aux-cache; \
7078
{{ ) end -}}
7179
\
7280
wget -O memcached.tar.gz "$MEMCACHED_URL"; \
@@ -78,6 +86,10 @@ RUN set -eux; \
7886
cd /usr/src/memcached; \
7987
\
8088
gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
89+
SOURCE_DATE_EPOCH="$(find . -type f -exec stat -c '%Y' {} + | sort -nr | head -n1)"; \
90+
export SOURCE_DATE_EPOCH; \
91+
# for logging validation/edification
92+
date --date "@$SOURCE_DATE_EPOCH" --rfc-2822; \
8193
./configure \
8294
--build="$gnuArch" \
8395
--enable-extstore \
@@ -109,7 +121,7 @@ RUN set -eux; \
109121
| sort -u \
110122
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
111123
)"; \
112-
apk add --no-network --virtual .memcached-rundeps $runDeps; \
124+
apk add --no-network --virtual .memcached-rundeps=0 $runDeps; \
113125
apk del --no-network .build-deps; \
114126
{{ ) else ( -}}
115127
apt-mark auto '.*' > /dev/null; \
@@ -123,9 +135,13 @@ RUN set -eux; \
123135
| xargs -r apt-mark manual \
124136
; \
125137
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
138+
# clean up for reproducibility
139+
rm -rf /var/log/* /var/cache/ldconfig/aux-cache; \
126140
{{ ) end -}}
127141
\
128-
memcached -V
142+
memcached -V ;\
143+
# clean up for reproducibility
144+
rm -rf /tmp/*
129145

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

0 commit comments

Comments
 (0)