pub struct ImageBuilder { /* private fields */ }Expand description
Image builder - orchestrates the full build process
ImageBuilder provides a fluent API for configuring and executing
container image builds using buildah as the backend.
§Build Process
- Parse Dockerfile (or use runtime template)
- Resolve target stages if specified
- Build each stage sequentially:
- Create working container from base image
- Execute each instruction
- Commit intermediate stages for COPY –from
- Commit final image with tags
- Push to registry if configured
- Clean up intermediate containers
§Cache Backend Integration (requires cache feature)
When a cache backend is configured, the builder can store and retrieve cached layer data to speed up subsequent builds:
use zlayer_builder::ImageBuilder;
let builder = ImageBuilder::new("./my-app").await?
.with_cache_dir("/var/cache/zlayer")
.tag("myapp:latest");Implementations§
Source§impl ImageBuilder
impl ImageBuilder
Sourcepub async fn new(context: impl AsRef<Path>) -> Result<Self>
pub async fn new(context: impl AsRef<Path>) -> Result<Self>
Create a new ImageBuilder with the given context directory
The context directory should contain the Dockerfile (unless using a runtime template) and any files that will be copied into the image.
§Arguments
context- Path to the build context directory
§Errors
Returns an error if:
- The context directory does not exist
- Buildah is not installed or not accessible
§Example
use zlayer_builder::ImageBuilder;
let builder = ImageBuilder::new("./my-project").await?;Sourcepub fn with_executor(
context: impl AsRef<Path>,
executor: BuildahExecutor,
) -> Result<Self>
pub fn with_executor( context: impl AsRef<Path>, executor: BuildahExecutor, ) -> Result<Self>
Create an ImageBuilder with a custom buildah executor
This is useful for testing or when you need to configure the executor with specific storage options.
Sourcepub fn dockerfile(self, path: impl AsRef<Path>) -> Self
pub fn dockerfile(self, path: impl AsRef<Path>) -> Self
Set a custom Dockerfile path
By default, the builder looks for a file named Dockerfile in the
context directory. Use this method to specify a different path.
§Example
let builder = ImageBuilder::new("./my-project").await?
.dockerfile("./my-project/Dockerfile.prod");Sourcepub fn zimagefile(self, path: impl AsRef<Path>) -> Self
pub fn zimagefile(self, path: impl AsRef<Path>) -> Self
Set a custom ZImagefile path
ZImagefiles are a YAML-based alternative to Dockerfiles. When set, the builder will parse the ZImagefile and convert it to the internal Dockerfile IR for execution.
§Example
let builder = ImageBuilder::new("./my-project").await?
.zimagefile("./my-project/ZImagefile");Sourcepub fn runtime(self, runtime: Runtime) -> Self
pub fn runtime(self, runtime: Runtime) -> Self
Use a runtime template instead of a Dockerfile
Runtime templates provide pre-built Dockerfiles for common development environments. When set, the Dockerfile option is ignored.
§Example
use zlayer_builder::{ImageBuilder, Runtime};
let builder = ImageBuilder::new("./my-node-app").await?
.runtime(Runtime::Node20);Sourcepub fn build_arg(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn build_arg(self, key: impl Into<String>, value: impl Into<String>) -> Self
Add a build argument
Build arguments are passed to the Dockerfile and can be referenced
using the ARG instruction.
§Example
let builder = ImageBuilder::new("./my-project").await?
.build_arg("VERSION", "1.0.0")
.build_arg("DEBUG", "false");Sourcepub fn build_args(self, args: HashMap<String, String>) -> Self
pub fn build_args(self, args: HashMap<String, String>) -> Self
Set multiple build arguments at once
Sourcepub fn target(self, stage: impl Into<String>) -> Self
pub fn target(self, stage: impl Into<String>) -> Self
Set the target stage for multi-stage builds
When building a multi-stage Dockerfile, you can stop at a specific stage instead of building all stages.
§Example
// Dockerfile:
// FROM node:20 AS builder
// ...
// FROM node:20-slim AS runtime
// ...
let builder = ImageBuilder::new("./my-project").await?
.target("builder")
.tag("myapp:builder");Sourcepub fn tag(self, tag: impl Into<String>) -> Self
pub fn tag(self, tag: impl Into<String>) -> Self
Add an image tag
Tags are applied to the final image. You can add multiple tags. The first tag is used as the primary image name during commit.
§Example
let builder = ImageBuilder::new("./my-project").await?
.tag("myapp:latest")
.tag("myapp:v1.0.0")
.tag("registry.example.com/myapp:v1.0.0");Sourcepub fn no_cache(self) -> Self
pub fn no_cache(self) -> Self
Disable layer caching
When enabled, all layers are rebuilt from scratch even if they could be served from cache.
Note: Currently this flag is tracked but not fully implemented in the
build process. ZLayer uses manual container creation (buildah from,
buildah run, buildah commit) which doesn’t have built-in caching
like buildah build does. Future work could implement layer-level
caching by checking instruction hashes against previously built layers.
Sourcepub fn layers(self, enable: bool) -> Self
pub fn layers(self, enable: bool) -> Self
Enable or disable layer caching
This controls the --layers flag for buildah. When enabled (default),
buildah can cache and reuse intermediate layers.
Note: ZLayer currently uses manual container creation (buildah from,
buildah run, buildah commit) rather than buildah build, so this
flag is reserved for future use when/if we switch to buildah build.
§Example
let builder = ImageBuilder::new("./my-project").await?
.layers(false) // Disable layer caching
.tag("myapp:latest");Sourcepub fn cache_from(self, registry: impl Into<String>) -> Self
pub fn cache_from(self, registry: impl Into<String>) -> Self
Set registry to pull cache from
This corresponds to buildah’s --cache-from flag, which allows
pulling cached layers from a remote registry to speed up builds.
Note: ZLayer currently uses manual container creation (buildah from,
buildah run, buildah commit) rather than buildah build, so this
option is reserved for future implementation.
TODO: Implement remote cache support. This would require either:
- Switching to
buildah buildcommand which supports –cache-from natively - Implementing custom layer caching with registry pull for intermediate layers
§Example
let builder = ImageBuilder::new("./my-project").await?
.cache_from("registry.example.com/myapp:cache")
.tag("myapp:latest");Sourcepub fn cache_to(self, registry: impl Into<String>) -> Self
pub fn cache_to(self, registry: impl Into<String>) -> Self
Set registry to push cache to
This corresponds to buildah’s --cache-to flag, which allows
pushing cached layers to a remote registry for future builds to use.
Note: ZLayer currently uses manual container creation (buildah from,
buildah run, buildah commit) rather than buildah build, so this
option is reserved for future implementation.
TODO: Implement remote cache support. This would require either:
- Switching to
buildah buildcommand which supports –cache-to natively - Implementing custom layer caching with registry push for intermediate layers
§Example
let builder = ImageBuilder::new("./my-project").await?
.cache_to("registry.example.com/myapp:cache")
.tag("myapp:latest");Sourcepub fn cache_ttl(self, ttl: Duration) -> Self
pub fn cache_ttl(self, ttl: Duration) -> Self
Set maximum cache age
This corresponds to buildah’s --cache-ttl flag, which sets the
maximum age for cached layers before they are considered stale.
Note: ZLayer currently uses manual container creation (buildah from,
buildah run, buildah commit) rather than buildah build, so this
option is reserved for future implementation.
TODO: Implement cache TTL support. This would require either:
- Switching to
buildah buildcommand which supports –cache-ttl natively - Implementing custom cache expiration logic for our layer caching system
§Example
let builder = ImageBuilder::new("./my-project").await?
.cache_ttl(Duration::from_secs(3600 * 24)) // 24 hours
.tag("myapp:latest");Sourcepub fn push(self, auth: RegistryAuth) -> Self
pub fn push(self, auth: RegistryAuth) -> Self
Sourcepub fn push_without_auth(self) -> Self
pub fn push_without_auth(self) -> Self
Enable pushing without authentication
Use this for registries that don’t require authentication (e.g., local registries, insecure registries).
Sourcepub fn default_registry(self, registry: impl Into<String>) -> Self
pub fn default_registry(self, registry: impl Into<String>) -> Self
Set a default OCI/WASM-compatible registry to check for images.
When set, the builder will probe this registry for short image names
before qualifying them to docker.io. For example, if set to
"git.example.com:5000" and the ZImagefile uses base: "myapp:latest",
the builder will check git.example.com:5000/myapp:latest first.
Sourcepub fn squash(self) -> Self
pub fn squash(self) -> Self
Squash all layers into a single layer
This reduces image size but loses layer caching benefits.
Sourcepub fn format(self, format: impl Into<String>) -> Self
pub fn format(self, format: impl Into<String>) -> Self
Set the image format
Valid values are “oci” (default) or “docker”.
Sourcepub fn default_cache_mounts(self, mounts: Vec<RunMount>) -> Self
pub fn default_cache_mounts(self, mounts: Vec<RunMount>) -> Self
Set default cache mounts to inject into all RUN instructions
Sourcepub fn with_events(self, tx: Sender<BuildEvent>) -> Self
pub fn with_events(self, tx: Sender<BuildEvent>) -> Self
Set an event sender for TUI progress updates
Events will be sent as the build progresses, allowing you to display a progress UI or log build status.
§Example
use zlayer_builder::{ImageBuilder, BuildEvent};
use std::sync::mpsc;
let (tx, rx) = mpsc::channel::<BuildEvent>();
let builder = ImageBuilder::new("./my-project").await?
.tag("myapp:latest")
.with_events(tx);Sourcepub async fn build(self) -> Result<BuiltImage>
pub async fn build(self) -> Result<BuiltImage>
Run the build
This executes the complete build process:
- Parse Dockerfile or load runtime template
- Build all required stages
- Commit and tag the final image
- Push to registry if configured
- Clean up intermediate containers
§Errors
Returns an error if:
- Dockerfile parsing fails
- A buildah command fails
- Target stage is not found
- Registry push fails
Auto Trait Implementations§
impl Freeze for ImageBuilder
impl RefUnwindSafe for ImageBuilder
impl Send for ImageBuilder
impl Sync for ImageBuilder
impl Unpin for ImageBuilder
impl UnsafeUnpin for ImageBuilder
impl UnwindSafe for ImageBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more