Skip to content

Container Image Build Strategy: Local Build vs. Multi-stage and Registry Cache

Interactive Diagram

새 탭 ↗

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.
PlantUML Diagram
FROM eclipse-temurin:17-jre
COPY target/myapp.jar /app/myapp.jar
CMD ["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.
# Stage 1: Build
FROM maven:3.9-eclipse-temurin-17 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn -q dependency:go-offline
COPY src ./src
RUN mvn -q clean package -DskipTests
# Stage 2: Run
FROM eclipse-temurin:17-jre
COPY --from=build /app/target/myapp.jar /app/myapp.jar
CMD ["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 copy src. 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 openjdk image is deprecated, so use a maintained distribution such as eclipse-temurin.
Local Build (Copy)Multi-stage Build
Pipeline steps4 (separate Build Artifacts)3
Build environmentBuild tools run directly on the runnerInside a Docker container
DependenciesJDK/Maven required on the runnerOnly Docker required
CacheBuild-tool cache (~/.m2, etc.) managed separatelyUnified as Docker layer cache
ReproducibilityDepends on the runner environmentHigh
  • 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.

PlantUML Diagram
- 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=registry reads and writes the cache in the registry. ref is 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, so max is 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-container driver, which setup-buildx-action sets up.
  • Cost and size: mode=max stores 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-to to the same ref, only the last build’s cache survives. Let only the default branch write, with other branches reading via cache-from, or separate the ref per branch.
  • Missed security patches: a long-lived cache can keep an old base image and delay OS patches. In scheduled builds, use --pull to fetch a fresh base image. --no-cache only skips the build cache and does not guarantee a new base pull, so use --pull --no-cache together 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.