Build an image

Build a Dockerfile or steps in a Space as pinned checkpoints that Spaces fork from, with a content-hash layer cache.

The call behind cua build: each filesystem step is a pinned checkpoint, a rebuild runs only what changed, and Spaces.fork(tag, name) or SpaceCreateOptions.build starts a Space from the result. See Build an image from a Dockerfile.

Build#

Methods of Spaces.

Spaces.build#

Builds an image from a Dockerfile or steps in a Space, as pinned layer checkpoints on this machine. Layers already built are reused (a rebuild of the same recipe is a cache hit that runs nothing; a changed step rebuilds from it on), and identical builds in flight share one run. The result's checkpoint is a fork source: Spaces.fork(result.tag or result.checkpoint, name) starts a Space from it in well under a second, with the image's environment, working directory and user. A failed step is an error that names the step, its exit code and its last output; earlier layers stay cached.

async def build(self, options: BuildOptions) -> BuildResult
ParameterTypeDefault
optionsBuildOptionsrequired

Returns BuildResult · Async · Raises CuaError

Spaces.build_with_progress#

Spaces.build, reporting each step and the lines it prints to listener as they happen.

async def build_with_progress(self, options: BuildOptions, listener: BuildListener) -> BuildResult
ParameterTypeDefault
optionsBuildOptionsrequired
listenerBuildListenerrequired

Returns BuildResult · Async · Raises CuaError

Spaces.builds#

The builds on this machine, oldest first, with their tags.

async def builds(self) -> List[BuildInfo]

Returns Vec<BuildInfo> · Async · Raises CuaError

Spaces.delete_build#

Deletes a build: its tags and the layer checkpoints no other build uses. build is a tag, a recipe key (or its first 12 characters) or a checkpoint. Returns the checkpoints deleted. Spaces already made from it keep running.

async def delete_build(self, build: str) -> List[str]
ParameterTypeDefault
buildStringrequired

Returns Vec<String> · Async · Raises CuaError

BuildOptions record#

Options for Spaces.build and super.SpaceCreateOptions.build. Give the recipe as a dockerfile (text) or a base image; steps are applied after it.

FieldTypeDefaultDescription
dockerfileOption<String>NoneDockerfile text. One stage; the instructions cua build documents (multi-stage, heredocs, BuildKit flags and ONBUILD are refused, naming the line).
context_dir / contextDirOption<String>NoneThe build context: the directory COPY sources are relative to (.dockerignore applies). Default: none, and a COPY fails.
baseOption<String>NoneA base image, for a build made of steps alone: a Cua image, ubuntu:<version>, debian:<version> or python:<version>.
stepsVec<BuildStep>[]Steps, after the Dockerfile's.
build_args / buildArgsHashMap<String, String>[:]Build arguments (ARG values).
tagOption<String>NoneAn alias for the result: Spaces.fork, build:<tag> images and Spaces.create(build=...) find the build by it.
kindOption<String>Nonevm (default), container or microvm.
onOption<String>NoneWhere: local (default). A build's checkpoints stay on the machine that took them.
cpusOption<u32>NonevCPUs of the build's machine, and so of Spaces made from it (default 2).
memory_mb / memoryMbOption<u64>NoneMemory in MiB (default 4096).
headlessOption<bool>NoneNo desktop: only cua-spacesd runs (default true).
no_cache / noCacheboolfalseBuild every layer again instead of using cached ones.
step_timeout_ms / stepTimeoutMsOption<u64>NoneBudget for each step in milliseconds (default one hour).

BuildStep enum#

One build step. Values are literal: a Dockerfile's $VARs are expanded by BuildOptions.dockerfile's parser, not here. Run and Copy steps run as the User, in the Workdir and with the Env set before them.

BuildStep.RUN(command: str)
BuildStep.RUN_EXEC(argv: List[str])
BuildStep.COPY(sources: List[str], dest: str, owner: Optional[str], mode: Optional[str], unpack: bool)
BuildStep.ENV(vars: dict[str, str])
BuildStep.WORKDIR(path: str)
BuildStep.USER(user: str)
BuildStep.EXPOSE(ports: List[str])
BuildStep.CMD(argv: List[str])
BuildStep.ENTRYPOINT(argv: List[str])
BuildStep.SHELL(argv: List[str])
BuildStep.LABEL(labels: dict[str, str])
BuildStep.VOLUME(paths: List[str])
BuildStep.STOP_SIGNAL(signal: str)
BuildStep.HEALTHCHECK(text: str)
VariantDescription
RunA shell command line (/bin/sh -c, or the last Shell). Fields: command: String
RunExecA command and arguments, run without a shell. Fields: argv: Vec<String>
CopyFiles of the build context copied in (COPY; unpack: ADD). Fields: sources: Vec<String>, dest: String, owner: Option<String>, mode: Option<String>, unpack: bool
EnvEnvironment for later steps and for commands run in the image. Fields: vars: HashMap<String, String>
WorkdirThe working directory (created when missing). Fields: path: String
UserThe user later steps and the image's commands run as. Fields: user: String
ExposePorts the image listens on. Fields: ports: Vec<String>
CmdThe image's default command. Fields: argv: Vec<String>
EntrypointThe image's entrypoint. Fields: argv: Vec<String>
ShellThe shell Run uses. Fields: argv: Vec<String>
LabelImage labels. Fields: labels: HashMap<String, String>
VolumeVolume mount points. Fields: paths: Vec<String>
StopSignalThe stop signal. Fields: signal: String
HealthcheckThe health check, as written. Fields: text: String

BuildResult record#

The result of Spaces.build.

Returned by Spaces.build, Spaces.build_with_progress.

FieldTypeDefaultDescription
checkpointStringThe final layer's checkpoint: Spaces.fork and cua sb fork start Spaces from it.
tagOption<String>The tag, when one was asked for.
keyStringThe recipe's cache key.
cachedboolEvery layer was cached: nothing ran.
stepsVec<BuildStepResult>One per step, base first.
configBuildImageConfigWhat commands run in the image get.
secondsf64Seconds the build took.

BuildStepResult record#

One step of a finished build.

FieldTypeDefaultDescription
indexu32The step (0: the base image).
textStringThe step as a line (RUN make).
cachedboolServed from a cached layer: nothing ran.
secondsf64Seconds it took (0 when cached).
checkpointOption<String>The layer's checkpoint, for a step that makes one.

BuildImageConfig record#

What an image configures for commands run in it.

FieldTypeDefaultDescription
baseStringThe FROM reference.
envHashMap<String, String>Environment from ENV steps.
workdirOption<String>Working directory, when a step set one.
userOption<String>User, when a step set one.
driver_user / driverUserStringThe user cua-spacesd runs as in the image.
cmdOption<Vec<String>>Default command.
entrypointOption<Vec<String>>Entrypoint.
shellOption<Vec<String>>The shell Run used.
exposedVec<String>Exposed ports.
labelsHashMap<String, String>Labels.
volumesVec<String>Volumes.
stop_signal / stopSignalOption<String>Stop signal.
healthcheckOption<String>Health check, as written.

BuildInfo record#

A build on this machine (Spaces.builds).

Returned by Spaces.builds.

FieldTypeDefaultDescription
checkpointStringThe final layer's checkpoint.
keyStringThe recipe's cache key.
tagsVec<String>Tags naming it.
baseStringThe FROM reference.
layersVec<String>Layers it is made of (checkpoints), base first.
created_at / createdAtStringRFC 3339 build time.
configBuildImageConfigImage configuration.

BuildProgress record#

What a build is doing (Spaces.build_with_progress).

FieldTypeDefaultDescription
eventStringnote, step, output, step_done or finished.
indexu32The step (0: the base image).
totalu32Steps after the base.
textStringThe step as a line, a note, or an output line.
cachedboolstep: served from a cached layer. finished: nothing ran.
secondsf64step_done and finished: seconds taken.
checkpointStringstep_done: the layer's checkpoint.

BuildListener#

Receives BuildProgress while Spaces.build_with_progress runs, in order, ending with finished. Called on an SDK worker thread; return quickly.

You implement BuildListener and pass it to the SDK (a callback interface): subclass it in Python, implement the interface in TypeScript, the BuildListener protocol in Swift, the interface in Kotlin and the trait in Rust.

MethodDescription
on_progressThe build moved on.

BuildListener.on_progress#

The build moved on.

def on_progress(self, progress: BuildProgress) -> None
ParameterTypeDefault
progressBuildProgressrequired

All items#

8 items, across this page and its subpages, grouped by page (each item's section is on its page). Methods are listed on their object.

Build an image: BuildImageConfig, BuildInfo, BuildListener, BuildOptions, BuildProgress, BuildResult, BuildStep, BuildStepResult