zlayer-agent
Container runtime agent for ZLayer, providing lifecycle management for containers, WebAssembly modules, and jobs.
Features
The agent supports multiple container runtimes through feature flags:
- youki (default on Linux): Linux-native container runtime using libcontainer. Preferred on Linux for performance (no daemon overhead).
- seatbelt (default on macOS): Native macOS sandbox runtime using Seatbelt (
sandbox-exec). Preferred for native Darwin workloads. - libkrun-vm (optional, macOS): Lightweight VM-based runtime using libkrun, used when full Linux compatibility is required on macOS.
- hcs-runtime (default on Windows): Native Windows containers via the Host Compute Service (HCS) APIs.
- wsl (default on Windows for Linux images): Delegates Linux-image workloads to a WSL2-hosted agent.
- docker: Cross-platform fallback using the Docker daemon. Works on Linux, Windows, and macOS.
- wasm: WebAssembly runtime using wasmtime for executing WASM workloads with WASI support.
Windows feature flags
The canonical Windows release build enables both HCS and WSL2 delegation:
On Windows, the agent uses a CompositeRuntime (src/runtimes/composite.rs) that inspects the OCI image manifest's OS field and dispatches each container to the appropriate backend: HcsRuntime for Windows images and Wsl2DelegateRuntime for Linux images. Docker remains available as a fallback when neither native runtime is suitable.
Installation
Add to your Cargo.toml:
[]
= { = "../zlayer-agent" }
# Or with specific features
= { = "../zlayer-agent", = ["docker", "wasm"] }
Runtime Selection
Automatic Selection
Use RuntimeConfig::Auto to automatically select the best available runtime:
use ;
let runtime = create_runtime.await?;
Selection logic:
- Linux: Prefers youki (libcontainer) if available, falls back to Docker.
- macOS: Prefers the Seatbelt sandbox runtime for native Darwin workloads, uses the libkrun VM runtime when full Linux compatibility is required, and falls back to Docker.
- Windows: Uses the
CompositeRuntimeto dispatch per image:HcsRuntimefor Windows containers andWsl2DelegateRuntime(requires thewslfeature) for Linux containers. Docker remains available as a fallback. Activated via--features hcs-runtime,wsl. - WASM artifacts: Use
RuntimeConfig::Wasmexplicitly (auto-detected from the OCI manifest where applicable).
Runtime matrix
| Runtime | Platform | Default? |
|---|---|---|
| youki | Linux | Yes (Linux) |
| seatbelt | macOS | Yes (macOS) |
| libkrun VM | macOS | Optional |
| HCS | Windows | Yes (Windows containers) |
| WSL2 delegate | Windows + wsl feature |
Yes (Linux containers on Windows) |
| composite | Windows | Auto-selected |
| docker | All | Fallback |
| wasm | All | Auto-detected from OCI manifest |
Explicit Selection
use ;
// Use youki runtime
let youki = create_runtime.await?;
// Use Docker runtime
let docker = create_runtime.await?;
// Use WASM runtime
WebAssembly Runtime Support
The wasm feature enables running WebAssembly modules with WASI support as an alternative to traditional containers.
Enabling WASM Support
[]
= { = "../zlayer-agent", = ["wasm"] }
WASM Runtime Configuration
use ;
use PathBuf;
use Duration;
let config = WasmConfig ;
let runtime = new.await?;
WASI Support
The WASM runtime supports WASI Preview 1 (wasip1) with the following capabilities:
- Environment variables
- Command-line arguments
- Stdout/stderr output
- Exit codes via
proc_exit
WASM vs Container Runtime Comparison
| Feature | Container (youki/seatbelt/libkrun/HCS/WSL2/Docker) | WASM |
|---|---|---|
| Isolation | Process/cgroup, Seatbelt sandbox, VM, or HCS silo | In-process sandbox |
| Startup time | 100ms+ | <10ms |
| Memory overhead | Higher (full rootfs) | Lower (just module) |
exec support |
Yes | No |
| Cgroup stats | Yes (where the host supports it) | No (stub values) |
| Filesystem mounts | Yes | Limited |
| Network | Full | Limited |
| Platform | Linux (youki), macOS (seatbelt/libkrun), Windows (HCS + WSL2 delegate), Docker fallback on all | Cross-platform |
WASM Limitations
- No
execsupport: WASM modules are single-process and don't support executing additional commands - No cgroup stats: WASM runs in-process without kernel isolation, so resource stats return stub values
- Limited filesystem: Currently only environment variables are passed; filesystem mounts are not yet supported
- Single entry point: WASM modules must export
_startormainfunction
Example: Running a WASM Workload
use ;
use ServiceSpec;
use Duration;
async
Service Manager
The ServiceManager provides higher-level orchestration for services:
use ;
use DeploymentSpec;
// Load deployment spec
let spec: DeploymentSpec = from_str?;
// Create service manager with auto-selected runtime
let manager = new.await?;
// Deploy services
for in &spec.services
Testing
Running All Tests
# Unit tests
# With Docker runtime tests
# With WASM runtime tests
# All features
WASM-Specific Tests
The WASM tests use inline WAT (WebAssembly Text Format) modules compiled at test time, so no external WASM binaries are required.
Module Structure
runtime.rs: AbstractRuntimetrait andContainerStatetypesruntimes/: Runtime implementationsyouki.rs: Linux-native runtime using libcontainerseatbelt.rs: macOS Seatbelt sandbox runtimelibkrun.rs: macOS libkrun VM runtime for full Linux compatibility (feature:libkrun-vm)hcs.rs: Windows Host Compute Service runtime for Windows containers (feature:hcs-runtime)wsl2_delegate.rs: Windows runtime that delegates Linux containers to a WSL2-hosted agent (feature:wsl)composite.rs:CompositeRuntimethat inspects the OCI image OS and dispatches toHcsRuntimeorWsl2DelegateRuntimeon Windowsdocker.rs: Docker daemon runtime (feature:docker)wasm.rs: WebAssembly runtime using wasmtime (feature:wasm)
service.rs:ServiceManagerfor higher-level orchestrationcontainer_supervisor.rs: Container health monitoring and restart logicjob.rs: Job execution support for run-to-completion workloadscron_scheduler.rs: Cron-based job schedulinghealth.rs: Health check implementationsinit.rs: Init action orchestrationenv.rs: Environment variable resolutionbundle.rs: OCI bundle creation for youki
Environment Variables
| Variable | Description | Default |
|---|---|---|
ZLAYER_WASM_CACHE_DIR |
WASM module cache directory | /var/lib/zlayer/wasm |
ZLAYER_RUNTIME_DIR |
Runtime state directory | /var/run/zlayer |
ZLAYER_ROOT_DIR |
Root directory for bundles | /var/lib/zlayer |
License
Apache-2.0