-
Notifications
You must be signed in to change notification settings - Fork 20
fix: pass SOURCE_DATE_EPOCH as build arg (+ fix timestamp in export metadata) for deterministic docker images
#285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SOURCE_DATE_EPOCH for deterministic images
SOURCE_DATE_EPOCH for deterministic images3f03680 to
133c4a2
Compare
SOURCE_DATE_EPOCH as build arg for Dockerfile access
6754212 to
994b111
Compare
SOURCE_DATE_EPOCH as build arg for Dockerfile access133c4a2 to
a48f97d
Compare
994b111 to
be57173
Compare
150ceb3 to
3772afa
Compare
SOURCE_DATE_EPOCH as build arg for deterministic images
3772afa to
9f59f6d
Compare
kylos101
approved these changes
Nov 19, 2025
kylos101
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥳
408e6b0 to
214d02b
Compare
SOURCE_DATE_EPOCH as build arg for deterministic imagesSOURCE_DATE_EPOCH as build arg (+ fix timestamp in export metadata) for deterministic images
SOURCE_DATE_EPOCH as build arg (+ fix timestamp in export metadata) for deterministic imagesSOURCE_DATE_EPOCH as build arg (+ fix timestamp in export metadata) for deterministic docker images
geropl
approved these changes
Nov 20, 2025
Member
geropl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes LGTM ✔️
be57173 to
015b47f
Compare
…mages Pass SOURCE_DATE_EPOCH as a Docker build arg to enable deterministic Docker image timestamps. Dockerfiles MUST declare ARG SOURCE_DATE_EPOCH for BuildKit to use the timestamp for image metadata: FROM alpine:3.18 ARG SOURCE_DATE_EPOCH Without this ARG declaration, images will have non-deterministic timestamps even though the environment variable is set (from PR #284). With the ARG, BuildKit uses SOURCE_DATE_EPOCH for: - Image metadata timestamps (created field) - History timestamps - OCI annotations The ARG is also available in RUN commands for custom build logic: RUN go build -ldflags "-X main.BuildTime=$SOURCE_DATE_EPOCH" -o app Co-authored-by: Ona <no-reply@ona.com>
Use getDeterministicMtime() for BuildTime in docker-export-metadata.json instead of time.Now() to ensure deterministic metadata files. This makes the docker-export-metadata.json file reproducible across builds with the same source code, reducing non-determinism in exported Docker image cache archives. The timestamp is derived from: - Git commit timestamp (normal case) - SOURCE_DATE_EPOCH env var (override) - Returns 0 in test environments Co-authored-by: Ona <no-reply@ona.com>
214d02b to
4b74f63
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Pass
SOURCE_DATE_EPOCHas a Docker build arg to enable deterministic Docker image timestamps.Fixes https://linear.app/ona-team/issue/CLC-2097/improve-builds-determinism
Critical Requirement
Dockerfiles MUST declare
ARG SOURCE_DATE_EPOCHfor BuildKit to use the timestamp for image metadata. Without this declaration, images will have non-deterministic timestamps even though the environment variable is set.How It Works
SOURCE_DATE_EPOCHfor build commands #284 exportsSOURCE_DATE_EPOCHas an environment variable--build-arg SOURCE_DATE_EPOCH=...to docker buildARG SOURCE_DATE_EPOCHto activate deterministic timestampsRequired Dockerfile Change
Add
ARG SOURCE_DATE_EPOCHafter theFROMstatement:Important: The ARG must be declared after
FROMbecause build args are scoped to the build stage. For multi-stage builds, declare it in each stage that needs it:With this ARG declaration, BuildKit uses
SOURCE_DATE_EPOCHfor:createdfield)Additional Use Cases
The ARG is also available in RUN commands for custom build logic:
Additional Fix: Deterministic Metadata
This PR also fixes a bug where
docker-export-metadata.jsonwas usingtime.Now()instead of the deterministic timestamp, making the cache tar.gz non-reproducible.Before:
{ "build_time": "2025-11-19T19:18:59.116392757Z" // Different each build }After:
{ "build_time": "2025-11-19T18:56:46Z" // Deterministic from git commit }Note: The
docker saveoutput (image.tar) still has non-deterministic tar metadata. This will be addressed in a future PR. However, the Docker image itself (image ID and layers) remains fully deterministic.Verification
Check BuildKit Version
Ensure you have BuildKit >= v0.13.0 for full SOURCE_DATE_EPOCH support:
docker buildx version # Should show: github.com/docker/buildx v0.13.0 or laterFor Docker Engine without buildx:
Verify Deterministic Builds
Build the same package twice and compare checksums:
Migration Guide
For teams with many Dockerfiles, use this script to add
ARG SOURCE_DATE_EPOCH:For multi-stage Dockerfiles, manually review and add ARG to each stage as needed.
Terminology Clarification
Deterministic images: The Docker image itself (layers, metadata, image ID) is identical across builds with the same source code. This is what BuildKit's SOURCE_DATE_EPOCH provides.
Reproducible cache: The leeway cache tar.gz file is also deterministic, meaning the same source produces the same cache artifact. This includes:
docker-export-metadata.json(fixed in this PR)docker saveoutput may still have minor tar metadata differences, but the image content is identicalTesting
Verified with test Dockerfile:
Changes
SOURCE_DATE_EPOCHas a build arg inbuildDockerfunctionmtimevalue (derived from git commit timestamp)docker-export-metadata.jsonto use deterministic timestamp instead oftime.Now()References
SOURCE_DATE_EPOCHfor build commands #284 (export SOURCE_DATE_EPOCH environment variable)