Skip to content

fix(docker): keep the runtime layers cached across updates - #704

Open
jedrzejsieracki wants to merge 1 commit into
BaaRai:mainfrom
jedrzejsieracki:fix/dockerfile-build-cache
Open

jedrzejsieracki wants to merge 1 commit into
BaaRai:mainfrom
jedrzejsieracki:fix/dockerfile-build-cache

Conversation

@jedrzejsieracki

Copy link
Copy Markdown

Problem

Every Docker update rebuilds the whole runtime stage, even when only Go code changed.

Since 869b44a the runtime stage declares ARTEX_VERSION and ARTEX_REVISION right after FROM, before its first RUN. A declared build argument is part of the cache key of every RUN after it, and update.sh writes the new commit into .env on every update. So each update reinstalls the apt packages, pip packages, Node, bbot and Playwright, which takes 6 to 11 minutes depending on the machine.

It also costs disk. Each rebuild leaves another copy of about 8 GB of layers in the build cache (bbot 3.5 GB, Playwright 2.3 GB, apt 1.6 GB, tools 0.4 GB). On one 63 GB host, four such copies had piled up and BuildKit's cache cleanup kept evicting entries close to its minimum-free-space threshold.

Change

  • The two ARGs and the LABEL that uses them move to the end of the runtime stage. The image still carries org.opencontainers.image.version / revision, but a new value now only re-stamps the label.
  • BuildKit cache mounts keep three caches between builds: the Go build cache (/root/.cache/go-build), the Next.js build cache (/src/web/.next/cache) and the npm download cache (/root/.npm). A code change recompiles only the affected packages and pages. The caches are not part of the image. The Dockerfile already uses # syntax=docker/dockerfile:1, which supports --mount.

go mod download keeps its normal layer, so the module set still comes from go.mod/go.sum exactly as before.

Testing

Measured with docker compose build artex on two hosts. Each "new commit" build passes a different ARTEX_VERSION/ARTEX_REVISION, the way update.sh does. "Before" is the current main with a warm cache.

Build Host A: 16 cores, overlay2 Host B: 8 cores, containerd image store
New commit, no code change, before 351s 664s
New commit, no code change, after 4s 11s
New commit + one-line Go change, after 8s (Go compile 2.9s, was 16s) 47s (Go compile 13s, was 41s)
New commit + one-line web change, after 13s (next build 6.6s, was 22s) not measured
First build after this change (fills the caches) 353s 696s

Host B is slower mainly because of image export and unpacking with the containerd image store (about 2 minutes for the 4.5 GB image before this change).

Also checked:

  • The built image still has the correct org.opencontainers.image.version and revision labels.
  • The build stage still receives the version and revision, so the -X main.version / main.revision linker flags are unchanged.

The cache mounts total about 0.8 GB, which is within BuildKit's default 48h / ~920 MB budget for cache mounts. If the budget is exceeded, BuildKit drops the mount and the next build compiles from scratch, which is the same as today.

🤖 Generated with Claude Code

@BaaRai

BaaRai commented Dec 1, 2026

Copy link
Copy Markdown
Owner

The cache analysis is correct. Please rebase on main (the Dockerfiles changed for the arm64 TARGETARCH fix) and re-run CI; I will review the Dockerfile diff after that.

@jedrzejsieracki
jedrzejsieracki force-pushed the fix/dockerfile-build-cache branch from 1ad7422 to 1ee5bdd Compare December 2, 2026 12:04
The runtime stage declared ARTEX_VERSION and ARTEX_REVISION before its
first RUN. A declared build argument is part of the cache key of every
later RUN, and update.sh writes the new commit into .env, so every
update rebuilt the whole stage: apt packages, bbot, Playwright. That
cost 6 to 11 minutes per update and left another ~8 GB copy of the
layers in the build cache. Declare the arguments and the LABEL that
uses them at the end of the stage instead.

Also keep the Go build cache, the Next.js build cache and the npm
download cache between builds with BuildKit cache mounts, so a code
change recompiles only what changed. The caches are not part of the
image.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jedrzejsieracki
jedrzejsieracki force-pushed the fix/dockerfile-build-cache branch from 1ee5bdd to 0158dbd Compare December 2, 2026 12:05
@jedrzejsieracki

Copy link
Copy Markdown
Author

Rebased on main (including the arm64 TARGETARCH change); CI is green.

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