Skip to content

Commit c5d365c

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 `httpd` binary reproducible. - GNU implementation of `wget` is executed with `--no-hsts` to disable creating `/root/.wget-hsts` - 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` 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.0-beta1 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.0-beta1/docs/build-repro.md> - To reproduce Debian packages, reproducers may: - modify the `RUN` instructions in the Dockerfile to rewrite `/etc/apt/sources.list` to use <http://snapshot.debian.org>, and restore `/etc/apt/sources.list` at the end of the instruction (See the rejected PR 248) - or, specify a custom Dockerfile frontend implementation that rewrites/restores `/etc/apt/sources.list` - or, specify a custom `HTTP_PROXY` that redirects HTTP requests for <http://deb.debian.org> to <http://snapshot.debian.org>. This is less reliable in long-term due to `Acquire::Check-Valid-Until`. Signed-off-by: Akihiro Suda <[email protected]>
1 parent 02a2b56 commit c5d365c

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

2.4/Dockerfile

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
FROM debian:bookworm-slim
22

3+
# The global SOURCE_DATE_EPOCH is consumed by commands that are not associated with a source artifact.
4+
# This is not propagated from --build-arg: https://github.com/moby/buildkit/issues/4576#issuecomment-2159501282
5+
ENV SOURCE_DATE_EPOCH 0
6+
37
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
48
#RUN groupadd -r www-data && useradd -r --create-home -g www-data www-data
59

@@ -20,7 +24,9 @@ RUN set -eux; \
2024
# https://github.com/docker-library/httpd/issues/209
2125
libldap-common \
2226
; \
23-
rm -rf /var/lib/apt/lists/*
27+
rm -rf /var/lib/apt/lists/* ; \
28+
# clean up for reproducibility
29+
rm -rf /var/log/* /var/cache/ldconfig/aux-cache
2430

2531
ENV HTTPD_VERSION 2.4.59
2632
ENV HTTPD_SHA256 ec51501ec480284ff52f637258135d333230a7d229c3afa6f6c2f9040e321323
@@ -68,7 +74,7 @@ RUN set -eux; \
6874
# if the version is outdated, we have to pull from the archive
6975
https://archive.apache.org/dist/ \
7076
; do \
71-
if wget -O "$f" "$distUrl$distFile" && [ -s "$f" ]; then \
77+
if wget --no-hsts -O "$f" "$distUrl$distFile" && [ -s "$f" ]; then \
7278
success=1; \
7379
break; \
7480
fi; \
@@ -170,7 +176,7 @@ RUN set -eux; \
170176
local patchSha256="$1"; shift; \
171177
ddist "$patchFile" "httpd/patches/apply_to_$HTTPD_VERSION/$patchFile"; \
172178
echo "$patchSha256 *$patchFile" | sha256sum -c -; \
173-
patch -p0 < "$patchFile"; \
179+
patch --set-utc --force -p0 < "$patchFile"; \
174180
rm -f "$patchFile"; \
175181
done; \
176182
}; \
@@ -180,6 +186,10 @@ RUN set -eux; \
180186
CFLAGS="$(dpkg-buildflags --get CFLAGS)"; \
181187
CPPFLAGS="$(dpkg-buildflags --get CPPFLAGS)"; \
182188
LDFLAGS="$(dpkg-buildflags --get LDFLAGS)"; \
189+
SOURCE_DATE_EPOCH="$(find . -type f -exec stat -c '%Y' {} + | sort -nr | head -n1)"; \
190+
export SOURCE_DATE_EPOCH; \
191+
# for logging validation/edification
192+
date --date "@$SOURCE_DATE_EPOCH" --rfc-2822; \
183193
./configure \
184194
--build="$gnuArch" \
185195
--prefix="$HTTPD_PREFIX" \
@@ -225,7 +235,9 @@ RUN set -eux; \
225235
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
226236
\
227237
# smoke test
228-
httpd -v
238+
httpd -v; \
239+
# clean up for reproducibility
240+
rm -rf /var/log/* /var/cache/ldconfig/aux-cache
229241

230242
# https://httpd.apache.org/docs/2.4/stopping.html#gracefulstop
231243
STOPSIGNAL SIGWINCH

2.4/alpine/Dockerfile

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
FROM alpine:3.20
22

3+
# The global SOURCE_DATE_EPOCH is consumed by commands that are not associated with a source artifact.
4+
# This is not propagated from --build-arg: https://github.com/moby/buildkit/issues/4576#issuecomment-2159501282
5+
ENV SOURCE_DATE_EPOCH 0
6+
37
# ensure www-data user exists
48
RUN set -x \
59
&& adduser -u 82 -D -S -G www-data www-data
@@ -35,7 +39,7 @@ ENV HTTPD_PATCHES=""
3539
# see https://httpd.apache.org/docs/2.4/install.html#requirements
3640
RUN set -eux; \
3741
\
38-
apk add --no-cache --virtual .build-deps \
42+
apk add --no-cache --virtual .build-deps=0 \
3943
apr-dev \
4044
apr-util-dev \
4145
coreutils \
@@ -181,13 +185,17 @@ RUN set -eux; \
181185
local patchSha256="$1"; shift; \
182186
ddist "$patchFile" "httpd/patches/apply_to_$HTTPD_VERSION/$patchFile"; \
183187
echo "$patchSha256 *$patchFile" | sha256sum -c -; \
184-
patch -p0 < "$patchFile"; \
188+
patch --set-utc --force -p0 < "$patchFile"; \
185189
rm -f "$patchFile"; \
186190
done; \
187191
}; \
188192
patches $HTTPD_PATCHES; \
189193
\
190194
gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
195+
SOURCE_DATE_EPOCH="$(find . -type f -exec stat -c '%Y' {} + | sort -nr | head -n1)"; \
196+
export SOURCE_DATE_EPOCH; \
197+
# for logging validation/edification
198+
date --date "@$SOURCE_DATE_EPOCH" --rfc-2822; \
191199
./configure \
192200
--build="$gnuArch" \
193201
--prefix="$HTTPD_PREFIX" \
@@ -219,7 +227,7 @@ RUN set -eux; \
219227
| sort -u \
220228
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
221229
)"; \
222-
apk add --no-network --virtual .httpd-so-deps $deps; \
230+
apk add --no-network --virtual .httpd-so-deps=0 $deps; \
223231
apk del --no-network .build-deps; \
224232
\
225233
# smoke test

0 commit comments

Comments
 (0)