快速开始
本指南假设 a3s-box --version 和 a3s-box info 已成功执行。尚未安装时,请先阅读
安装指南。
检查主机能力
输出会显示主机架构、虚拟化后端、客体通道和运行时路径。只有主机能够强制执行请求的 配置时,工作负载才会被准入。
在 MicroVM 中运行命令
省略 --isolation 即选择默认独立内核路径:
Box 会在需要时拉取 OCI 镜像、准备根文件系统、启动 MicroVM、传递工作负载退出码, 并删除已经终止的盒子。
管理长时间运行的服务
名称是便捷查询条件;运行时内部使用稳定盒子 ID 和代际围栏,拒绝陈旧生命周期操作。
持久化数据
创建并挂载命名卷:
绑定挂载、tmpfs、提交、导出和文件系统快照见存储与快照。
显式选择共享内核 Sandbox
在经过认证的 Linux 主机上:
显式的 --isolation microvm 会被拒绝;省略参数才是默认 MicroVM 的公开选择方式。
这样脚本不会误把后端名称当作可互换的兼容模式。
使用原生 SDK
以下四个示例都是完整程序:创建 Sandbox、执行客体命令、输出结果并清理资源。
TypeScript 生命周期
向下滚动或点击步骤,右侧会显示对应代码并高亮当前操作。
运行步骤创建 MicroVM
import { Sandbox } from '@a3s-lab/box';async function main(): Promise<void> {const sandbox = await Sandbox.create('alpine:3.20');await sandbox.kill();}main().catch((error: unknown) => {console.error(error);process.exitCode = 1;});
运行步骤设置资源和网络
import { Sandbox } from '@a3s-lab/box';async function main(): Promise<void> {const sandbox = await Sandbox.create('alpine:3.20', {cpus: 2,memoryMb: 1024,network: { mode: 'none' },});await sandbox.kill();}main().catch((error: unknown) => {console.error(error);process.exitCode = 1;});
运行步骤写入工作文件
import { Sandbox } from '@a3s-lab/box';async function main(): Promise<void> {const sandbox = await Sandbox.create('alpine:3.20', {cpus: 2,memoryMb: 1024,network: { mode: 'none' },});try {await sandbox.files.write('/workspace/status.txt', 'ready\n');console.log(await sandbox.files.read('/workspace/status.txt'));} finally {await sandbox.kill();}}main().catch((error: unknown) => {console.error(error);process.exitCode = 1;});
运行步骤运行命令并检查结果
import { Sandbox } from '@a3s-lab/box';async function main(): Promise<void> {const sandbox = await Sandbox.create('alpine:3.20', {cpus: 2,memoryMb: 1024,network: { mode: 'none' },});try {await sandbox.files.write('/workspace/status.txt', 'ready\n');const result = await sandbox.commands.run(['test', '-s', '/workspace/status.txt'],{ timeoutMs: 30_000 },);if (result.exitCode !== 0) {throw new Error(result.stderr);}console.log(result.stdout);} finally {await sandbox.kill();}}main().catch((error: unknown) => {console.error(error);process.exitCode = 1;});
运行步骤始终清理 Sandbox
import { Sandbox } from '@a3s-lab/box';async function main(): Promise<void> {const sandbox = await Sandbox.create('alpine:3.20', {cpus: 2,memoryMb: 1024,network: { mode: 'none' },});try {await sandbox.files.write('/workspace/status.txt', 'ready\n');const result = await sandbox.commands.run(['test', '-s', '/workspace/status.txt'],{ timeoutMs: 30_000 },);if (result.exitCode !== 0) {throw new Error(result.stderr);}console.log(result.stdout);} finally {await sandbox.kill();}}main().catch((error: unknown) => {console.error(error);process.exitCode = 1;});
SDK 总览说明了四种语言共享的契约,并链接到每种语言的完整指南。
平台提示
- Linux/KVM 提供最完整的运行时能力。
- macOS 仅支持 Apple Silicon,使用 HVF。
- Windows x86_64 使用 WHPX;桥接网络、启动后
exec、内存快照、TEE、Compose 工作负载和 CRI 具有明确限制。
依赖平台特性前,请检查平台矩阵和 Windows 指南。