Runtime images#

An Image is the runtime filesystem and environment in which a Yak Task executes. Your Python source is only part of that environment: the image supplies the Python runtime, installed Python packages, operating-system packages, files created during the build and non-secret environment variables.

An Image is not a separate service that you start or manage. Yak prepares the selected image before the Task runs, then executes the Task with the prepared runtime environment.

The mental model#

You do not need a different image for every Task. An App can provide one default image, and a task can override that choice when a particular Task needs a different runtime. Yak can use an existing registry reference or prepare an image from a fluent Image definition or supported Dockerfile.

Think of the relationship like this:

text
base image
 + Python packages
 + operating-system packages
 + environment variables
 + build commands
 + Dockerfile/context inputs
        ↓
image definition
        ↓
Yak prepares or resolves the runtime artifact
        ↓
Tasks execute with that artifact

Image preparation is separate from each Task invocation. A package install or build command runs while the image is being prepared, not every time the Task is called.

Start from a Python image#

The simplest choice for a Python Task is one of Yak's Python convenience constructors:

python
image = yak.Image.python_3_11()

The current release exposes python_3_11 , python_3_12 and python_3_13 . Use the constructor that matches the Python version your code requires; the constructor owns the underlying base-image details.

Attach the image to the whole App when its Tasks share one runtime:

python
app = yak.App(
    name="example",
    image=image,
)

If neither the App nor a task specifies an image, the current default is the python:3.11 registry image. An explicit App(image=...) choice is useful when you want the Python family or another registry reference to be part of the application declaration.

yak.Image.debian_slim() is also available as a plain Debian slim base. It does not select a Python family, so use it only when that base is appropriate for the runtime you are building. The generated Image reference is authoritative for the complete constructor and member surface.

Supported Python runtimes#

Yak Preview currently supports CPython 3.11, 3.12 and 3.13 on glibc-based linux/amd64 runtimes. Yak checks the actual interpreter and runtime ABI inside the selected image before executing your Task.

Other Python versions, Python implementations, operating-system architectures and musl-based images such as Alpine are not currently supported. Yak rejects an unsupported runtime before user code starts rather than attempting to run it with incompatible Python packages.

For the simplest supported setup, use Image.python_3_11(), Image.python_3_12(), Image.python_3_13() or an equivalent glibc-based registry image. For example, python:3.12-alpine is deliberately rejected by the current runtime boundary.

A complete dependency example#

The canonical example rendered below uses requests==2.31.0, a package outside the Python standard library. It defines the image once, attaches it to the App, imports the package inside the remote Task and prints the installed version from an App entrypoint.

Keep imports for image-provided packages inside the Task when the package is not installed in your local development environment. The example source is owned by src/content/examples/custom_image.py, so the page and executable example stay in sync.

Install Python packages#

Use the fluent builder to add package specifications to a Python image:

python
image = (
    yak.Image.python_3_11()
    .pip_install(
        "requests==2.31.0",
        "numpy==1.24.0",
    )
)

Each package specification becomes part of the remote image. When the Task executes, those packages are available to its Python runtime. The builder is chainable, so you can add more image operations after pip_install.

The current pip_install implementation accepts package strings plus these keyword options: index_url, extra_index_url, find_links and no_index. These options are intentionally bounded; arbitrary pip flags are not forwarded. For example, an unsupported keyword such as timeout is rejected rather than silently becoming part of the build. See Image.pip_install for the exact public signature and types.

Use a package specification for dependencies that belong to Python. If a dependency needs a system library or executable, add the operating-system package separately with apt_install or use a Dockerfile.

Install operating-system packages#

Use apt_install for Debian-style system packages:

python
image = (
    yak.Image.python_3_11()
    .apt_install("git", "curl")
)

pip_install changes the Python environment; apt_install installs system packages into the image filesystem. apt_install runs the image's apt-get command, so it assumes an apt-compatible base/runtime. Do not treat it as a portable package-manager abstraction for arbitrary registry images.

See Image.apt_install for the exact public signature.

Set non-secret environment variables#

Use env for ordinary configuration that should be part of the image:

python
image = (
    yak.Image.python_3_11()
    .env("SERVICE_MODE", "preview")
    .env("PYTHONUNBUFFERED", "1")
)

Image environment variables have both build and runtime meaning. Later build commands receive the current image environment, and the completed image records those values so the Task receives them at runtime.

Normal non-sensitive runtime configuration may belong in an image environment variable. Credentials, tokens and passwords should use Yak Secrets instead:

Do not place sensitive values in env, a Dockerfile ENV, a RUN command or the source archive. See Image.env for the exact method contract.

Run image build commands#

Use run_commands for a build step that is not expressed by pip_install or apt_install:

python
image = (
    yak.Image.python_3_11()
    .run_commands("mkdir -p /opt/example")
    .run_commands("printf 'ready\\n' > /opt/example/status")
)

Commands run in order in the image build environment and see the filesystem changes made by earlier build steps. Files they create and changes they make are part of the resulting image. They do not run again for every Task invocation.

Prefer pip_install and apt_install when they express the intent clearly. Shell-heavy build steps are harder to validate and are more likely to fail because of quoting, missing executables or assumptions about the base image. See Image.run_commands for the exact method contract.

App-level versus Task-level images#

App(image=...) defines the default runtime for Tasks in that App. A task-level image= overrides the App choice for that Task when you pass an Image object:

python
default_image = yak.Image.python_3_11()

app = yak.App(
    name="image-overrides",
    image=default_image,
)


@app.task()
def uses_default() -> str:
    return "python 3.11 image"


special_image = yak.Image.python_3_12()


@app.task(image=special_image)
def uses_override() -> str:
    return "python 3.12 image"


@app.task(image="python:3.13-slim")
def uses_registry_override() -> str:
    return "direct registry image"

Tasks without an explicit task image inherit the App image. If neither declaration supplies one, they use the current default python:3.11 registry image. The App declaration and task decorator are documented in the generated App reference .

The pinned App.task contract accepts str | Image | None. A task-level string selects that task's registry image directly; a task-level Image object supplies a custom image definition. Both override the App image during ordinary Task registration. Use the direct string form when the registry image already contains everything the task needs, and an Image object when Yak must prepare packages or build steps.

Use an existing registry image#

When a public registry image already contains the runtime you need and no Yak builder steps are required, pass its reference directly to the App:

python
app = yak.App(
    name="registry-runtime",
    image="python:3.11-slim",
)

The same App-level string form can use a digest-pinned reference:

python
app = yak.App(
    name="reproducible-runtime",
    image="registry.example/team/service@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
)

Tags are convenient but can move. A digest-pinned reference makes the source choice explicit and is useful when you want a stable registry input. Yak resolves the image it will execute and uses the resolved artifact as execution authority for that lifecycle operation.

Image.from_registry is useful when the registry image should be the base of an Image definition:

python
image = (
    yak.Image.from_registry("python:3.11-slim")
    .pip_install("requests==2.31.0")
)

When from_registry(...) is attached as an Image object, it enters Yak's builder path, even if no additional builder method is added. Use the direct string form when you want an existing registry runtime without Yak-side builder steps. Use Image.from_registry for the exact constructor contract.

The current Image request does not provide arbitrary private-registry credentials. Do not assume that a private registry reference works merely because a local Docker client can pull it. The current Preview supports registry references that Yak can resolve through its configured registry path; private-registry build mode is not available through this SDK request shape.

Build from a Dockerfile#

Use Image.from_dockerfile when the runtime is already expressed as a Dockerfile and build context:

python
image = yak.Image.from_dockerfile("./Dockerfile")

app = yak.App(
    name="dockerfile-runtime",
    image=image,
)

With no second argument, the build context defaults to the directory containing the Dockerfile. The following is a minimal Dockerfile using only the currently supported instruction forms:

dockerfile
FROM python:3.11-slim
WORKDIR /app
RUN pip install requests==2.31.0

Yak supports a bounded Dockerfile subset, not arbitrary Docker compatibility. The current subset includes:

  • one FROM as the first instruction, with one image reference and no alias or flags;
  • RUN commands, in order, without instruction flags;
  • ENV key/value assignments;
  • WORKDIR with one path;
  • COPY with one source and one destination, without flags;
  • LABEL key/value assignments; and
  • USER with a simple user name, numeric UID or numeric UID:GID.

Comments and backslash line continuations are normalized as part of this supported parser surface. Dockerfile instructions are applied in order against one build environment.

The following are currently unsupported and fail before an image is produced:

  • multi-stage builds, a second FROM, FROM aliases and FROM flags;
  • parser directives such as # syntax= and # escape=;
  • ADD, EXPOSE, CMD, ENTRYPOINT, VOLUME, ARG and ONBUILD;
  • instruction flags such as RUN --mount, including BuildKit secret mounts;
  • COPY flags, JSON-array form, source globs and path forms outside the bounded context grammar; and
  • arbitrary Dockerfile instructions not listed in the supported subset.

Use Image.from_dockerfile for the exact Python signature, including the optional context directory. If the Dockerfile needs a feature outside this subset, start from a supported registry image and express the supported steps with the fluent builder where possible.

Docker build context and .dockerignore#

The Dockerfile is interpreted relative to a build context. The context is the directory containing the files that COPY may use:

text
project/
├── Dockerfile
├── requirements.txt
├── app.py
└── .dockerignore

For this layout, a Dockerfile can use:

dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt

The COPY source must be a relative POSIX path inside the context, must exist and must not be excluded by .dockerignore. Yak rejects context traversal, escaping symlinks and unsupported context entries. Current COPY source forms do not include arbitrary globs; use explicit files or directories. COPY destinations use the bounded absolute POSIX form, such as /app/requirements.txt or /app/.

.dockerignore is applied before the context is submitted. The current implementation supports blank lines and comments, ordered ignore rules, ! negation, *, **, ?, slash-qualified paths and directory-only patterns ending in /. Character-class patterns such as [abc] are not supported by Yak's deterministic parser. A file that is ignored cannot be a COPY source.

Keep build contexts small and use .dockerignore to exclude unrelated source, build artifacts, virtual environments and local caches. Yak snapshots the selected context for the build submission, so a source file changing outside the selected context does not change that context input, while a selected file changing does.

Use the current Image surface#

The pinned release schema exposes only supported Image operations. Older Preview examples may mention copy_local(), imports() or local image identity/context helpers; those names are not public members of the current Image reference.

Use a Dockerfile with COPY and a bounded build context for local files. Put image-provided imports inside the remote Task, as in the canonical dependency example, and declare the dependency with pip_install. Do not build new code around removed helper methods or SDK-internal image serialization and context lifecycle methods.

Image definitions are reproducible inputs#

An Image definition is an input to image preparation, not a mutable runtime session. Its identity includes the selected base reference and the ordered builder instructions. Dockerfile Images also include the Dockerfile and selected context inputs.

The current control plane can reuse a succeeded build when the accepted definition matches. Otherwise it prepares the required artifact before the Task is admitted to execution. This enables reuse of identical definitions without promising a particular cache location, retention period or build duration.

Yak does not promise zero rebuild time, a specific cache locality, a specific cold-start time or compatibility with Docker's layer cache. Treat the definition as reproducible input and the resolved artifact as the runtime authority.

When image preparation happens#

The user-observable lifecycle is:

  • yak run preflights the source, starts the App bootstrap and registers SDK-declared Tasks. A custom Image is prepared during Task registration, and the CLI waits for registration to complete before invoking the entrypoint.
  • yak serve follows the same remote registration path before its endpoint session is available. Stopping the serve session affects the session lifecycle; it does not turn image preparation into a local service.
  • yak deploy resolves the App image as part of the persistent Live App submission. A custom Image is prepared before the Live App can report ready; a registry reference is resolved for the Live App candidate Version.

The CLI and SDK may show image status or build progress while registration or Live App preparation is in progress. If image preparation fails, the command reports the build or registry diagnostic and the user Task does not run. Common causes are listed below.

After a successful custom-image preparation, a later use of the same accepted definition may report reuse. Registry tags can be refreshed by a new lifecycle operation, while digest-pinned references keep the source choice stable.

Image failures#

Image failures happen before the user Task starts. The developer-visible causes include:

  • a package cannot be resolved or installed by pip_install;
  • an apt package cannot be resolved or installed;
  • a run_commands command exits unsuccessfully or assumes a missing executable;
  • the Dockerfile path does not exist, is unreadable or is outside its context;
  • an unsupported or invalid Dockerfile instruction is used;
  • a COPY source is missing, ignored, a glob, a traversal or an unsupported symlink;
  • a COPY destination uses an unsupported path form;
  • the build context contains an unsupported entry or a context file changes while it is being submitted; or
  • Yak cannot resolve, pull or materialize the selected registry image.

Fix the image definition or registry input first. A Task exception is a separate failure that occurs after the runtime image has already been prepared.

Images with Live Apps#

Persistent yak deploy supports one App-level Image. The currently supported App-level choices are:

  • a registry string such as "python:3.11-slim";
  • a fluent SDK Image, including a Python convenience image with builder steps; and
  • an Image.from_dockerfile(...) definition whose Dockerfile and context satisfy the supported subset.

That App-level image is used for the endpoint Tasks in a persistent Live App. A task-level @app.task(image=...) override is not a supported way to give one persistent endpoint a different runtime; the current Live App contract uses the App-level Image for its endpoint Tasks. Use the Live Apps guide for Live App lifecycle and Endpoint behavior.

This is a stricter persistent Live App boundary than ordinary Task registration, where task-level Image overrides are supported. Do not assume that every yak run image combination has a corresponding per-Endpoint yak deploy combination.

Choose an image approach#

  • Standard Python with third-party packages — start with Image.python_3_11() (or the required Python family) and add pip_install(...).
  • Python plus operating-system packages — add apt_install(...) to a Python image.
  • An existing registry runtime — pass a registry string to App(image=...).
  • A registry image that should be the base of further image definition — use Image.from_registry(...) and add supported builder steps.
  • An existing Dockerfile and build context — use Image.from_dockerfile(...).
  • One runtime shared by the App — set App(image=...).
  • One Task needing a different runtime — pass an Image object to @app.task(image=...).

For exact names, signatures, defaults, return types and the complete public member inventory, use the generated Image reference . The guide explains how to choose and combine the supported paths; the generated reference remains authoritative for API details.