Image building strategies
Image building turns an application source tree into a tagged deployment image and places that image in the configured registry. The builder command gives the build flow one common interface while choosing where and how to build for the required CPU architectures. It is one stage of the broader command execution flow and supplies the image later used by application deployment.
Builder command abstraction
The builder command is the common control layer for image creation. It exposes the same build, setup, inspection, cleanup, pull, and image-check actions to the CLI while routing each action to a selected strategy.
The shared build contract covers the parts that define the deployment image:
- Target CPU architectures.
- Dockerfile or buildpack source.
- Build context.
- Build arguments and build secrets.
- Cache use.
- Image tags and the service label.
- Optional provenance and software bill of materials data.
The selected strategy changes the build location and tool. It does not change the image handoff expected by deployment: the result is pushed to the registry, then pulled and checked on the application hosts. See image identity and registry for the image naming and registry contract.
Strategy selection
The configuration resolves the requested architectures into local and remote sets. A local builder handles architectures available on the current host. A remote or hybrid builder handles the remaining architectures when a remote endpoint is configured.
The effective choice follows these rules:
| Configuration shape | Strategy | Build location |
|---|---|---|
| Pack configuration | Pack | Local Pack builder |
| Cloud driver | Cloud | Docker Build Cloud |
| Remote endpoint with only remote architectures | Remote | Remote Docker host |
| Remote endpoint with local and remote architectures | Hybrid | Local and remote builder nodes |
| No remote or cloud selection | Local | Current Docker environment |
The local: false setting sends all requested architectures to the remote builder. Configuration rejects that setting when no remote endpoint exists. The Docker driver also cannot combine remote builders, multiple architectures, or build cache settings.
References: lib/…/builder.rb• L15-54 lib/…/builder.rb• L22-66 lib/…/builder.rb• L155-169 lib/…/builder.rb• L5-14
Local builders
The local strategy builds on the machine that runs the build command. It uses the configured Docker build driver and creates a named Buildx builder when the driver needs one. This is the normal path for a single architecture that matches the local host.
The local strategy can also build more than one architecture when the selected local driver supports it. A local registry adds local network access to the builder so the build can reach that registry.
References: lib/…/local.rb• L1-19 lib/…/base.rb• L19-29 lib/…/base.rb• L136-143
Remote builders
The remote strategy builds on a Docker host reached through the configured remote endpoint. The local command creates a Docker context for that host and attaches a Buildx builder to it. This keeps the build command local while moving image work to a host with the required architecture or resources.
Remote builds do not log in to the registry from the local machine. When the registry is local, the build flow forwards the registry port to the remote host so the remote builder can reach the same registry.
References: lib/…/remote.rb• L1-73 lib/…/build.rb• L87-121 lib/…/build.rb• L216-240
Cloud builders
The cloud strategy uses a Docker Build Cloud driver named by the configuration. It lets a managed cloud builder perform the build while the local command controls setup, inspection, and image export.
Cloud builds still use the common image tags and platform selection. They are useful when the build should run outside the local machine but does not need a user-managed SSH host.
References: lib/…/cloud.rb• L1-22 lib/…/builder.rb• L56-58 lib/…/builder.yml• L114-120
Hybrid builders
The hybrid strategy combines local and remote builder nodes into one multi-platform Buildx builder. The local node builds the local architecture set. The remote node builds the remaining set. Buildx then exports the combined result as the deployment image.
This strategy is selected when a remote endpoint exists and the requested architectures include both the local host architecture and another architecture. It gives one build command a single image result without forcing every platform through the remote host.
References: lib/…/hybrid.rb• L1-21 lib/…/builder.rb• L30-54 test/…/deploy_with_hybrid_builder.yml• L38-42
Pack builds
The Pack strategy builds the image with Cloud Native Buildpacks instead of a Dockerfile build. Pack detects and assembles the application from the source context by using the configured builder image and buildpacks. Kamal adds the service label so the result remains identifiable during deployment.
Pack builds support one architecture at a time. The build produces the normal image tags, then pushes them when the output type is the registry.
References: lib/…/pack.rb• L1-46 lib/…/builder.rb• L64-97 lib/…/builder.rb• L9-12 lib/…/builder.yml• L34-45
Git clone contexts
The clone context is a source strategy that gives a build a clean revision of the repository. When the builder has no explicit context and the project uses Git, the build flow clones the repository into a temporary directory, resets it to the selected revision, removes untracked files, and updates submodules before building.
This prevents local uncommitted or untracked files from changing a release image. An explicit context such as . opts into building from the current checkout instead.
References: lib/…/builder.rb• L138-153 lib/…/clone.rb• L1-32 lib/…/build.rb• L26-38
Image build lifecycle
The build lifecycle prepares access, selects a clean or explicit source context, creates or checks the builder, and exports the image. The later pull stage moves that image from the registry to the application hosts.
The main sequence is:
flowchart LR
A[Build command] --> B[Prepare registry and hooks]
B --> C[Prepare source context]
C --> D[Inspect or create builder]
D --> E[Build requested platforms]
E --> F[Push image tags]
F --> G[Pull on application hosts]
G --> H[Validate service label]
The build exports the versioned image and the latest image. The pull stage can seed registry mirrors before it pulls the image on the remaining hosts. A host accepts the result only when the image carries the expected service label.
References: lib/…/build.rb• L4-84 lib/…/build.rb• L189-195 lib/…/base.rb• L53-60 lib/…/base.rb• L75-83
Build inputs and shared controls
All Docker-based strategies use the same high-level build inputs. These inputs let a deployment choose its source and build behavior without changing the strategy interface.
| Input | Purpose |
|---|---|
arch | Selects the image platforms to produce. |
context | Selects the source directory. An omitted value enables the clean Git clone path when applicable. |
dockerfile | Selects the Dockerfile for Docker-based builds. |
target | Selects a named Dockerfile build stage. |
args and secrets | Pass build-time values. Secrets come from the deployment secret store. |
cache | Shares build layers through GitHub Actions or a registry. |
provenance and sbom | Controls build metadata and software inventory output. |
The builder passes the service name as an image label. That label links the build result to the service that deployment expects and prevents an unrelated image from being accepted on a host.
References: lib/…/builder.rb• L26-37 lib/…/builder.rb• L60-136 lib/…/base.rb• L45-60
Change checklist
When these files or sections are changed, remember to consider these:
- Keep strategy selection aligned with architecture splitting and the
local: falsevalidation rule. - Keep remote registry forwarding aligned with the endpoint used by remote and hybrid builders.
- Keep the service label contract aligned between image creation and host-side image validation.
- Keep Pack's single-architecture rule aligned with Pack platform selection.
- Keep clean clone behavior aligned with the meaning of an omitted build context.
- Keep builder names isolated by registry account so cached registry credentials are not shared across accounts.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Local builds are disabled without a remote endpoint | No strategy can produce the requested image | Configuration validation rejects the setup |
| A remote builder cannot be inspected or no longer matches its endpoint | The build cannot start on the intended host | The build flow removes and recreates the builder when the missing-builder condition is detected |
| A remote builder cannot reach a local registry | The remote build cannot export its image | The build flow forwards the local registry port to the remote host |
| The requested Dockerfile is missing | The build command fails before image creation | The builder checks the Dockerfile path |
| The image lacks the expected service label | A host rejects the image as unrelated to the service | Host-side validation checks the label before deployment continues |
Key files
| File | Why to read it |
|---|---|
| lib/…/builder.rb | Shows the common builder interface and strategy selection. |
| lib/…/builder.rb | Shows how architectures and builder settings become strategy inputs. |
| lib/…/base.rb | Shows the shared image build contract and image checks. |
| lib/…/local.rb | Shows the local build strategy. |
| lib/…/remote.rb | Shows the remote host and Docker context strategy. |
| lib/…/hybrid.rb | Shows the local and remote multi-platform strategy. |
| lib/…/cloud.rb | Shows the managed cloud builder strategy. |
| lib/…/pack.rb | Shows the Buildpacks-based strategy. |
| lib/…/clone.rb | Shows the clean Git source strategy. |
| lib/…/build.rb | Shows how image build and image pull form one delivery flow. |