External Sandboxes
Mount a full external sandbox into an agentOS VM for workloads that need a real Linux kernel.
When a workload needs a full Linux environment, attach an external sandbox to
a library VM with the sandbox option. External sandboxes rely on trusted host
callbacks and mounts, so they are not available on hosted VMs. agentOS mounts the sandbox filesystem into
the VM at /mnt/sandbox and registers agentos-sandbox host functions for
running commands and listing processes in the sandbox.
// Sandbox extension: mount a Docker sandbox filesystem and run commands.
//
// Requires Docker. Starts a sandbox-agent container, mounts its filesystem
// at /mnt/sandbox, and registers sandbox host functions for running commands.
import { AgentOs } from "@rivet-dev/agentos-core";
import { docker } from "@rivet-dev/agentos-sandbox";
const SANDBOX_QUICKSTART_PERMISSIONS = {
fs: "allow",
network: "allow",
childProcess: "allow",
env: "allow",
hostFunction: "allow",
} as const;
const skipDocker = process.env.SKIP_DOCKER === "1";
const SANDBOX_MOUNT = "/mnt/sandbox";
if (skipDocker) {
console.log("Skipping sandbox quickstart because SKIP_DOCKER=1.");
process.exit(0);
}
// Start a Docker-backed sandbox, mount its filesystem, and register its host functions.
const vm = await AgentOs.create({
permissions: SANDBOX_QUICKSTART_PERMISSIONS,
sandbox: { provider: docker() },
});
try {
// Write and read a file through the mounted sandbox filesystem.
await vm.filesystem.writeFile(
`${SANDBOX_MOUNT}/hello.txt`,
"Hello from agentOS!",
);
const content = await vm.filesystem.readFile(`${SANDBOX_MOUNT}/hello.txt`);
console.log("Read from sandbox mount:", new TextDecoder().decode(content));
const runCommandResult = await vm.process.exec(
"agentos-sandbox run-command --command echo --args 'hello from Docker sandbox'",
);
console.log("Sandbox command:", (runCommandResult.stdout ?? "").trim());
const processList = await vm.process.exec("agentos-sandbox list-processes");
console.log("Sandbox processes:", (processList.stdout ?? "").trim());
} finally {
await vm.dispose();
}
Guest code reads and writes sandbox files the same way it reads local files.
Providers live in @rivet-dev/agentos-sandbox.
The sandbox bridge runs through trusted host callbacks in the embedding process, so credentials for the sandbox provider stay on the host.
See agentOS vs Sandbox to decide when a workload needs one.