Skip to content

Fetch all sources in a download stage ahead of the build - #840

Closed
Dummgelaufen007 wants to merge 2 commits into
wader:masterfrom
Dummgelaufen007:downloader-stage
Closed

Dummgelaufen007 wants to merge 2 commits into
wader:masterfrom
Dummgelaufen007:downloader-stage

Conversation

@Dummgelaufen007

@Dummgelaufen007 Dummgelaufen007 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

52 recipes today start by fetching their own source: wget and sha256sum for 47 archives, git clone and git checkout for 5 git sources. These lines go away from the recipes, so there is less code, and one step at the start of the build fetches all sources at once. The layout of the Dockerfile stays as it is: each recipe keeps its ARGs and starts where it unpacks or enters its source.

Why

Every source is fetched and verified before anything is compiled. A server that is down or an archive that fails its checksum stops the build in its first minutes, not midway through the compile steps.

One download step that works the same for every recipe only has to be right once. Today every recipe has its own, and a typo in one of them shows up when the build reaches that recipe and then has to be found. Now it shows up before the first download, with the file and line to look at.

Every library keeps its arguments in one place

No ARG moves to the top of the file and there is no second list of sources. Version, URL, checksum and bump lines stay with their recipe, as they are today:

ARG VMAF_VERSION=3.2.1
ARG VMAF_URL="https://github.com/Netflix/vmaf/archive/refs/tags/v$VMAF_VERSION.tar.gz"
ARG VMAF_FILE="vmaf.$VMAF_VERSION.tar.gz"
ARG VMAF_SHA256=5df7386911bc15fd1ca783132528748d219768ae4fc5f8e0b61184f041648092
RUN \
  tar $TAR_OPTS $VMAF_FILE && cd vmaf-*/libvmaf && \

The download stage works its way through the Dockerfile being built and finds its work there: every <NAME>_URL, together with <NAME>_FILE and <NAME>_SHA256 for an archive, or <NAME>_COMMIT and <NAME>_DIR for a git source. A new library needs nothing in the download stage; its ARGs next to the recipe are enough. A bump touches the same lines as before.

How it works

Part What it does
COPY $DOCKERFILE /Dockerfile gives the download stage the Dockerfile being built, so it can read the ARGs where they stand
downloader stage picks the ARGs a download needs and walks over every <NAME>_URL. With a <NAME>_FILE it is an archive: fetched and checked against <NAME>_SHA256. Without one it is a git source: cloned treeless and checked out at <NAME>_COMMIT into <NAME>_DIR; a commit that is not a full hash is an error. fdk-aac is skipped unless ENABLE_FDKAAC is set
COPY --from=downloader /downloads/ / puts archives and clones where the recipes already expect them

Mistakes show up before the first download

A wrong setup should cost seconds, not a build, and the message should say where to look. Whoever adds a library or bumps one by hand learns within a second whether its ARGs are complete, instead of after the build has reached its recipe.

  • The wrong Dockerfile. Docker does not pass the name given with -f into the build, so someone who builds a renamed copy and forgets --build-arg DOCKERFILE=<name> would otherwise get the downloads of whatever file is called Dockerfile. The download stage therefore names the file it reads in its first log line, and stops at once if that file has no download stage.
  • Incomplete ARGs. A new library with a <NAME>_URL but no checksum yet, or a git source without its commit, would fail somewhere inside wget, sha256sum or git, with a message that does not say which recipe is wrong. All sources are checked before the first download, and each incomplete one is reported as <file>:<line>: ARGs of <NAME> incomplete, with the line of its <NAME>_URL.

Changes

File Change
Dockerfile downloader stage added before builder, and the COPY --from=downloader after the builder's apk add
Dockerfile 47 ARG <NAME>_FILE lines added, one per archive, each named <name>.<version>.<ext>, with the extension of its URL; the tar line unpacks $<NAME>_FILE
Dockerfile 5 ARG <NAME>_DIR lines added, one per git source, each named <repository>-<commit>; the recipe changes into it instead of a directory name written into its cd
Dockerfile wget and sha256sum removed from 47 recipes, git clone and git checkout from 5. <NAME>_URL, <NAME>_SHA256, <NAME>_VERSION and the bump lines are unchanged
Dockerfile X264_VERSION renamed to X264_COMMIT, in its bump line and in versions.json too: it holds a commit, and every other git source names that <NAME>_COMMIT
Dockerfile git sources are cloned with --filter=tree:0 --no-checkout instead of in full: every commit is still there, so a version counted from the history (x264 version.sh) stays the same, only the trees of older commits are left out. A server that does not support the filter, like git.ffmpeg.org over git://, ignores it and sends the full clone
Dockerfile WGET_OPTS gains --progress=dot:mega: all downloads now share one step

The checksum is now checked the same way for every archive.

Build args

Arg Default Meaning
DOCKERFILE Dockerfile the file the download stage copies and reads; set it when building another file with -f

Tested

Alpine 3.24.2, the download stage on its own (--target downloader) with ENABLE_FDKAAC=1: fetched and verified all 47 archives, fdk-aac included, and cloned the 5 git sources at their commits.

Both checks were tried on broken copies of the Dockerfile: a renamed file built without --build-arg DOCKERFILE, and sources with a missing checksum, commit, directory or file name, or with nothing but a URL. Each stopped the build before the first download. The first named the file it had read, the others the file and line of the source's <NAME>_URL.

Research and wording by Claude (Anthropic), directed and reviewed by me.

@wader wader left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea. Some things that makes me a bit reluctant:

  • Added complexity of the Dockerfile
  • Does any of the changes require modern docker version?
  • How big is the problem with failed downloads in practice?

Comment thread Dockerfile
ARG APK_OPTS=""
RUN apk add --no-cache $APK_OPTS bash wget git
COPY --from=downloads / /downloads/
ARG DOCKERFILE=Dockerfile

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would break if Dockerfile was named something else? if so is there way to know the name somehow? thinking about the case where ppl copy and rename it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is the one catch, and I found no way around it: Docker does not pass the name given with -f into the build. It is the price of the inline solution: the download stage reads the ARGs from the Dockerfile itself, so that every library keeps them next to its recipe instead of in a second list, and for that it needs the file's name. A renamed copy is built with -f <name> --build-arg DOCKERFILE=<name>.

If the build arg is forgotten, the build does not go on blindly. The download stage names the file it reads in its first log line, and stops before the first download if that file has no download stage. If there is no file of that name at all, the build stops right at the start with "/Dockerfile": not found.

Research and wording by Claude (Anthropic), directed and reviewed by me.

Comment thread Dockerfile Outdated
# bump: alpine /ALPINE_VERSION=alpine:([\d.]+)/ docker:alpine|^3
# bump: alpine link "Release notes" https://alpinelinux.org/posts/Alpine-$LATEST-released.html
ARG ALPINE_VERSION=alpine:3.24.2
FROM scratch AS downloads

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure i follow here, what's the use of the downloads target? the copy below copies no files (scratch) i think and even further down downloaded files are copied from the downloader image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, in a normal build it did nothing. It was there for an optional local cache of the sources, which made the PR more complicated than it needed to be and is of no use in CI, so I have taken it out.

Research and wording by Claude (Anthropic), directed and reviewed by me.

@Dummgelaufen007

Dummgelaufen007 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for looking at it, and you were right about the complexity. That was my fault: the first version did too much at once. I have reworked the download stage along KISS (Keep It Smart and Simple) and removed everything it did not need, so that it only fetches the sources.

Added complexity. The PR moves code more than it adds any: the download step that every recipe wrote out by hand now exists once, and the recipes start where they unpack or enter their source. A new library only needs its ARGs next to its recipe; the download stage does not change. One download step that works the same for every recipe only has to be right once, instead of looking for a typo in one of 52.

In lines:

  • 104 lines leave the recipes: their wget/sha256sum and git clone/git checkout lines.
  • The download stage is written once, with comments on why it does what it does, and two checks that stop a wrong setup before the first download: a wrong Dockerfile, and a source whose ARGs are incomplete, reported with the file and line to look at.
  • 52 lines are one ARG <NAME>_FILE or <NAME>_DIR per source. Today every recipe writes its archive name by hand three times, in wget -O, in the sha256sum line and in tar; now it is defined once, next to the URL and checksum it belongs to.

vmaf as an example, before:

ARG VMAF_VERSION=3.2.1
ARG VMAF_URL="https://github.com/Netflix/vmaf/archive/refs/tags/v$VMAF_VERSION.tar.gz"
ARG VMAF_SHA256=5df7386911bc15fd1ca783132528748d219768ae4fc5f8e0b61184f041648092
RUN \
  wget $WGET_OPTS -O vmaf.tar.gz "$VMAF_URL" && \
  echo "$VMAF_SHA256  vmaf.tar.gz" | sha256sum -c - && \
  tar $TAR_OPTS vmaf.tar.gz && cd vmaf-*/libvmaf && \
  meson setup build \

With the file name in VMAF_FILE, wget and echo only use the recipe's ARGs:

ARG VMAF_VERSION=3.2.1
ARG VMAF_URL="https://github.com/Netflix/vmaf/archive/refs/tags/v$VMAF_VERSION.tar.gz"
ARG VMAF_FILE="vmaf.$VMAF_VERSION.tar.gz"
ARG VMAF_SHA256=5df7386911bc15fd1ca783132528748d219768ae4fc5f8e0b61184f041648092
RUN \
  wget $WGET_OPTS -O "$VMAF_FILE" "$VMAF_URL" && \
  echo "$VMAF_SHA256  $VMAF_FILE" | sha256sum -c - && \
  tar $TAR_OPTS $VMAF_FILE && cd vmaf-*/libvmaf && \
  meson setup build \

So these two lines are the same for every archive, and the download stage runs them once for all of them. After:

ARG VMAF_VERSION=3.2.1
ARG VMAF_URL="https://github.com/Netflix/vmaf/archive/refs/tags/v$VMAF_VERSION.tar.gz"
ARG VMAF_FILE="vmaf.$VMAF_VERSION.tar.gz"
ARG VMAF_SHA256=5df7386911bc15fd1ca783132528748d219768ae4fc5f8e0b61184f041648092
RUN \
  tar $TAR_OPTS $VMAF_FILE && cd vmaf-*/libvmaf && \
  meson setup build \

Docker version. A plain docker build . still builds it, with no extra flags or tools, since Docker 23.0, where BuildKit became the default builder (release notes). The one newer feature it uses is the heredoc of the download stage, the "download/build helpers" idea from #374. Only the legacy builder (DOCKER_BUILDKIT=0) cannot build it.

Failed downloads in practice. They happen regularly, and today they show up late, after part of the build has already run. With the download stage, nothing is compiled until every source is downloaded and verified, so a failed download shows up after about 1.5 minutes. The stage does not make failures rarer, and it does not make the build slower: it runs alongside the builder's apk add, and the downloads it does are the ones the recipes did before.

I went through the failed CI and Multi arch runs whose logs are still kept, from 2026-07-14 to 2026-10-15: 21 of them failed on a download.

  • 15 were checksum mismatches: libbluray 6, libsrt 5, dav1d 3, libva 1, libvpl 1.
  • 6 were network errors: git.ffmpeg.org, code.videolan.org, cairographics.org.

These jobs ran for up to 29 minutes before they failed.

Research and wording by Claude (Anthropic), directed and reviewed by me.

@Dummgelaufen007

Copy link
Copy Markdown
Contributor Author

Closing in favour of #841.

Research and wording by Claude (Anthropic), directed and reviewed by me.

@Dummgelaufen007
Dummgelaufen007 deleted the downloader-stage branch October 26, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants