Skip to content

build(backend): cache Go modules and build output in Dockerfile.local - #9249

Merged
klesh merged 1 commit into
apache:mainfrom
DoDiODev:pr/dockerfile-local-buildcache
Oct 26, 2026
Merged

klesh merged 1 commit into
apache:mainfrom
DoDiODev:pr/dockerfile-local-buildcache

Conversation

@DoDiODev

Copy link
Copy Markdown
Contributor

Summary

Speed up repeated local builds of backend/Dockerfile.local (used by docker-compose-dev-mysql.yml and docker-compose-dev-postgresql.yml) by caching Go modules and the Go build cache with BuildKit.

Changes

Where Change
backend/Dockerfile.local download modules in a separate step that only depends on go.mod/go.sum
backend/Dockerfile.local BuildKit cache mounts for /go/pkg/mod and /root/.cache/go-build in the make mock, make swag, make build-plugin and make build-server steps

Without these changes, every source change invalidates COPY . /app, so all modules are downloaded again and all plugins are compiled from scratch. With the cache mounts, Go only rebuilds the packages that changed. backend/Dockerfile already uses the same go-build cache mount.

The image content is unchanged. The file is not used by CI or the release images, so CI does not exercise it.

Verification

Docker 29.5.3 (BuildKit), VM with 2 CPUs / 5 GB, main @ aaca6bd5c vs. this branch. "Rebuild" = one line appended to plugins/github/impl/impl.go (a real content change; touch alone does not invalidate COPY).

Check main this PR
Build from scratch (--no-cache, empty cache mounts) 434 s 529 s (includes the separate go mod download; noisy on a 2-CPU VM)
Rebuild after changing one plugin file 330 s 93 s
— make mock / make swag 47 s / 20 s 3 s / 4 s
— make build-plugin 211 s 72 s
— make build-server 28 s 6 s
Image: same file list under /app and /usr/local/lib — ✅ identical (51 files)
Run against PostgreSQL 18.6: GET /ping, GET /proceed-db-migration, 47 plugins loaded (GET /plugins), no panic/fatal ✅ ✅

The FROM … as builder casing is left untouched so this does not conflict with #9217.

Download modules in a separate layer keyed on go.mod/go.sum and use
BuildKit cache mounts for the module and build caches in the
mock/swag/plugin/server steps, so a source change no longer re-downloads
all modules and rebuilds every plugin from scratch.

Signed-off-by: DoDiODev <DoDiDev@proton.me>

@klesh klesh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM
Thanks for your contribution.

@klesh
klesh merged commit e32a6e7 into apache:main Oct 26, 2026
7 checks passed
@DoDiODev
DoDiODev deleted the pr/dockerfile-local-buildcache branch October 27, 2026 09:44
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