Run a sandbox
Give a luna its own Linux machine, run commands in it, read and write its files, size it, and choose what it keeps when it stops.
Some work needs a whole machine: a compiler, a test suite, a program that is not JavaScript. A sandbox is a Linux virtual machine that belongs to one luna, and that luna’s handlers drive it.
Run a command
Declare a sandbox under sandboxes with sandbox, then call exec on ctx.sandboxes.<name>. The sandbox starts on first use.
import { defineLuna, sandbox, t } from "luna";
export const User = defineLuna({
name: "User",
key: { id: t.string() },
sandboxes: {
box: sandbox({ image: "<IMAGE>" }),
},
handlers: (h) => ({
run: h({ args: { command: t.string() } }, async (ctx, { command }) => {
const result = await ctx.sandboxes.box.exec(command, { timeout: "30s" });
return { code: result.exitCode, out: result.stdout, err: result.stderr };
}),
}),
});
A string runs with /bin/sh -c, and an array such as ["node", "app.js"] runs as it is. exec waits for the command to end, and returns its exitCode, stdout and stderr.
| Option | Does |
|---|---|
cwd |
The working directory. /home by default |
env |
Environment variables to add |
timeout |
Stops the command, and everything it started, after this long. 5 minutes by default |
stdin |
Text or bytes written to its standard input |
timedOut is true when the timeout stopped it. truncated is true when output past 16 MiB on either stream was dropped. A call from an app waits at most 60 seconds, so start longer work as a process with Run processes in a sandbox.
Choose an image
Replace <IMAGE> with a public image in an OCI registry, pinned by its digest, such as ghcr.io/<OWNER>/<NAME>@sha256:<DIGEST>. A tag before the @ is only a note. A reference with a tag and no digest, such as ghcr.io/<OWNER>/<NAME>:latest, is refused when you deploy.
Luna pulls the image without credentials, so it must be public. A multi-platform image works, since Luna takes the build for the machine the sandbox runs on. An image may unpack to at most 32 GiB.
Size it
| Option | Does |
|---|---|
cpu |
Virtual CPUs. 1 by default |
memory |
Memory, as "512MB" or "2GB". "512MB" by default |
disk |
The size of /home, and of the writable layer over the image. "4GB" each by default |
idle |
Powers it off after this long with no request. "5m" by default |
A sandbox holds only the memory its programs use, and memory they free goes back. Limits gives the largest sandbox and how many may run at once.
Read and write its files
files reads and writes the /home volume. Paths are relative to /home, or absolute within it. This handler also copies the file to a builds bucket that User declares.
build: h({}, async (ctx) => {
const box = ctx.sandboxes.box;
await box.files.write("app/index.js", "console.log('hello')");
const r = await box.exec(["node", "app/index.js"]);
await box.files.copyTo(ctx.blobs.builds, "app/index.js", "builds/index.js", { contentType: "text/javascript" });
return r.stdout;
}),
write makes the folders a path needs and returns the file’s size. read, text, list, stat, remove and mkdir do what their names say, and stat returns null when nothing is there.
copyTo copies a file of up to 256 MiB to a bucket, and copyFrom copies a file from a bucket into the sandbox. Reads work while the sandbox is asleep, without starting it. Buckets are in Store files.
Choose what it keeps
persist says what is kept when the sandbox stops.
persist |
Keeps |
|---|---|
"filesystem" |
Its disks, /home and the writable layer. The default |
"freeze" |
Its disks, memory and running processes, so the next use carries on. When that cannot be restored, it starts from its disks |
"none" |
Nothing. Every start begins from the image |
Know when it runs
A sandbox stops after idle with no request, an open connection or a process that keeps it awake. Its luna stays awake while the sandbox runs, and can sleep once it stops.
status() tells its state without starting it. state is running, asleep, frozen or none, and stoppedBy says why it last stopped: idle, code, luna (its luna slept or moved) or crash.
const s = await ctx.sandboxes.box.status();
Resize it while it runs
Give cpu or memory a max to make the sandbox resizable. It starts at default, and resize changes it at once, or for the next start when it is stopped.
work: sandbox({ image: "<IMAGE>", cpu: { max: 8, default: 2 }, memory: { max: "16GB", default: "2GB" } }),
await ctx.sandboxes.work.resize({ cpu: 4, memory: "8GB" });
Memory can go from 256 MB up to max. A resize that would leave less memory than the sandbox is using is refused. The sandbox is billed at the size it is set to.
Run more than one
instance(name) is another sandbox of the same declaration, which starts on first use. A resizable declaration takes a size for a new instance. fork(name) makes an instance with a copy of this one’s disks as they are now.
const copy = await ctx.sandboxes.box.fork("experiment");
await copy.exec("git checkout -b experiment");
const all = await ctx.sandboxes.box.list();
list() returns every instance of the declaration, running, asleep or frozen.
Stop or delete it
stop() powers the sandbox off and keeps what persist says. freeze() saves its memory and processes with its disks. destroy() deletes it with everything kept of it.
luna sandboxes lists every sandbox in an environment, and stops or deletes one.
luna sandboxes list User
Choose a sandbox or an isolate
| Sandbox | Isolate | |
|---|---|---|
| Runs | Any Linux program | JavaScript only |
| Keeps | Its disks, by default | Nothing between runs |
| Lasts | Until idle or stopped | One run |
| Network | none, public or a list of names |
none, public or a list of names |
| Use it for | Builds, tests, dev servers, shells | Formulas, plugins, generated code |
Isolates are in Run untrusted code.
Next steps
- Run processes in a sandbox starts programs that run on after a call.
- Connect to a sandbox opens terminals, ports and SSH for people and apps.
- Reach the internet gives a sandbox a network.