Container Image Build Strategy: Local Build vs. Multi-stage and Registry Cache
Building container images in CI involves two decisions: where the application is built (on the CI runner or inside the Dockerfile), and where the cache lives across repeated builds. This post covers each decision in turn.
1. Build approach: Local Build vs. Multi-stage
Section titled “1. Build approach: Local Build vs. Multi-stage”- Local Build (Dockerfile Copy approach): the CI runner builds the artifact (a jar) with Maven or similar, and the Dockerfile only
COPYs the result. - Multi-stage Build: the Dockerfile contains a build stage, so a single Docker build handles everything from compiling the source to producing the runtime image.
Local Build (Copy approach)
Section titled “Local Build (Copy approach)”FROM eclipse-temurin:17-jreCOPY target/myapp.jar /app/myapp.jarCMD ["java", "-jar", "/app/myapp.jar"]- Pipeline: Checkout → Build Artifacts (separate step) → Image Build → Image Push
- The runner needs JDK and Maven. If tool versions differ between runners, the same commit can produce different artifacts.
Multi-stage Build
Section titled “Multi-stage Build”# Stage 1: BuildFROM maven:3.9-eclipse-temurin-17 AS buildWORKDIR /appCOPY pom.xml .RUN mvn -q dependency:go-offlineCOPY src ./srcRUN mvn -q clean package -DskipTests
# Stage 2: RunFROM eclipse-temurin:17-jreCOPY --from=build /app/target/myapp.jar /app/myapp.jarCMD ["java", "-jar", "/app/myapp.jar"]- Pipeline: Checkout → Image Build → Image Push
- Anything with Docker can reproduce the same result.
- Order matters: copy
pom.xml, fetch dependencies, then copysrc. When only the source changes, the dependency layer is reused from cache. In the reverse order, a one-line source change re-downloads every dependency. - Base the runtime image on a JRE rather than a JDK to reduce size and attack surface. The official
openjdkimage is deprecated, so use a maintained distribution such aseclipse-temurin.
Comparison and selection criteria
Section titled “Comparison and selection criteria”| Local Build (Copy) | Multi-stage Build | |
|---|---|---|
| Pipeline steps | 4 (separate Build Artifacts) | 3 |
| Build environment | Build tools run directly on the runner | Inside a Docker container |
| Dependencies | JDK/Maven required on the runner | Only Docker required |
| Cache | Build-tool cache (~/.m2, etc.) managed separately | Unified as Docker layer cache |
| Reproducibility | Depends on the runner environment | High |
- For new projects, make Multi-stage the default for portability and reproducibility.
- If a standardized build server already exists and the same artifact feeds several images, Local Build is simpler.
- Large Maven/Gradle projects with long build times benefit most when Multi-stage is combined with the registry cache below.
2. Cache location: container registry cache
Section titled “2. Cache location: container registry cache”GitHub-hosted runners start on a fresh VM every time, so no Docker layer cache survives. Even self-hosted runners scatter their cache across machines. A registry cache stores the BuildKit cache in a separate repository in the container registry, so builds on any runner and any branch can reuse the same cache.
GitHub Actions example
Section titled “GitHub Actions example”- name: Set up Docker Buildx uses: docker/setup-buildx-action@v3
- name: Build and push Container image uses: docker/build-push-action@v6 with: context: . file: ./Dockerfile push: true cache-from: type=registry,ref=${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-cache cache-to: type=registry,ref=${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-cache,mode=max tags: | ${{ vars.ACR_LOGIN_SERVER }}/${{ vars.ACR_REPOSITORY }}-${{ inputs.environment }}:${{ env.IMAGE_TAG }}- cache-from / cache-to:
type=registryreads and writes the cache in the registry.refis a dedicated cache repository. - mode=max: caches not only the final image layers but also the intermediate stages of a multi-stage build. The default,
mode=min, exports only layers in the final image, somaxis needed to keep the build-stage cache. vars.*references organization/repository variables, so the registry address isn’t hard-coded in the workflow.- Exporting a registry cache requires Buildx with the
docker-containerdriver, whichsetup-buildx-actionsets up.
Operational caveats
Section titled “Operational caveats”- Cost and size:
mode=maxstores intermediate layers too, so the cache repository grows quickly. Give the cache repository a different retention policy from deployment images. - Cache contention: if several branches write
cache-toto the sameref, only the last build’s cache survives. Let only the default branch write, with other branches reading viacache-from, or separate therefper branch. - Missed security patches: a long-lived cache can keep an old base image and delay OS patches. In scheduled builds, use
--pullto fetch a fresh base image.--no-cacheonly skips the build cache and does not guarantee a new base pull, so use--pull --no-cachetogether when package-install steps must also rerun. - Alternative: the GitHub Actions cache backend (
type=gha) serves the same purpose without registry setup. However, older entries can be evicted within the per-repository storage limit, so a registry cache is better for large images or when several repositories share the same base.