docs/examples/sandboxing/warm-workers.mdx
<Tooltip tip="This workflow creates and restores local disk snapshots, which are not available on microsandbox cloud."><span className="msb-badge-local">Local-only <Icon icon="circle-info" size={11} /></span></Tooltip>
Package installation often costs more than sandbox boot. This example installs OpenCode once, records the stopped sandbox as an integrity-checked snapshot, and launches fresh workers from that prepared filesystem.
Create the worker verification script that the snapshot will carry into every worker:
#!/bin/sh
set -eu
test "$(id -u)" -ne 0
git init -q
git add .
git -c user.name=microsandbox \
-c [email protected] \
commit -qm "sandbox baseline"
test -z "$(git remote)"
opencode --version
msb run --name agent-base --replace `
--cpus 2 --memory 2G --root-disk 4G `
--script-path verify-worker:./verify-worker.sh `
node:24-bookworm-slim -- sh -lc '
apt-get update &&
apt-get install -y --no-install-recommends ca-certificates git &&
rm -rf /var/lib/apt/lists/* &&
npm install -g [email protected] &&
mkdir -p /workspace/project &&
chown -R node:node /workspace &&
opencode --version
'
When the command exits, agent-base is stopped and ready to snapshot.
msb snapshot create coding-agent-base `
--from agent-base `
--integrity `
--force
Verify the captured snapshot before using it:
msb snapshot verify coding-agent-base
The snapshot captures the writable disk changes and pins the source image. It does not capture memory, running processes, network state, environment variables, or named volumes. Integrity verification detects later changes to those captured bytes; it does not attest who built the snapshot or whether its packages are trustworthy.
<Warning> Snapshots preserve every file written to the guest disk, including shell history, tool configuration, and cached credentials. Build the baseline in a trusted workflow, and never authenticate OpenCode or place registry tokens, source code, or API keys in `agent-base`. </Warning> </Step> <Step title="Launch a clean worker"> <CodeGroup> ```sh macOS & Linux msb run -d --name coding-worker-1 --replace \ --from-snapshot coding-agent-base \ --cpus 2 --memory 2G \ --user node --security restricted \ --max-duration 1h \ --workdir /workspace/project \ -- sh -lc 'exec sleep 1h' ```msb run -d --name coding-worker-1 --replace `
--from-snapshot coding-agent-base `
--cpus 2 --memory 2G `
--user node --security restricted `
--max-duration 1h `
--workdir /workspace/project `
-- sh -lc 'exec sleep 1h'
Transfer the committed project tree into the worker:
<CodeGroup> ```sh macOS & Linux git -C ./my-project archive --format=tar HEAD | \ msb exec --stream --user node \ --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 \ coding-worker-1 -- tar -x -C /workspace/project ```git -C ./my-project archive --format=tar --output=project.tar HEAD
msb cp ./project.tar coding-worker-1:/tmp/project.tar
msb exec --user node `
--rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 `
coding-worker-1 -- tar -x -f /tmp/project.tar -C /workspace/project
Remove-Item ./project.tar
Create a credential-free Git baseline and verify the worker boundary:
<CodeGroup> ```sh macOS & Linux msb exec --user node --workdir /workspace/project \ --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 \ coding-worker-1 -- verify-worker ```msb exec --user node --workdir /workspace/project `
--rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 `
coding-worker-1 -- verify-worker
Start OpenCode after those checks pass:
<CodeGroup> ```sh macOS & Linux msb exec -t --user node --workdir /workspace/project \ --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 \ coding-worker-1 -- opencode ```msb exec -t --user node --workdir /workspace/project `
--rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 `
coding-worker-1 -- opencode
Rootfs patches such as --copy-dir cannot be combined with --from-snapshot, so the worker boots first and receives the committed tree afterward. git archive excludes .git, checkout credentials, and untracked files such as a local .env; review the committed tree for secrets before sending it. Initializing a new repository inside the worker preserves useful diff workflows without copying host remotes or credentials. Each transferred or interactive workload sets its own process, file-descriptor, and per-file limits.
Each launch receives its own writable layer. Changes made by one worker do not modify the snapshot, the host project, or another worker.
<Note> This worker uses microsandbox's default public-internet profile so OpenCode can reach a configured provider. For sensitive projects, replace it with a deny-by-default allowlist for the provider and source hosts you need, and use [host-held secrets](/sandboxes/secrets) instead of copying credentials into the worker. </Note>Create more workers by changing the sandbox name:
<CodeGroup> ```sh macOS & Linux msb run --name coding-worker-2 --replace \ --from-snapshot coding-agent-base \ --user node --security restricted \ --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 \ --max-duration 1m \ -- opencode --version ```msb run --name coding-worker-2 --replace `
--from-snapshot coding-agent-base `
--user node --security restricted `
--rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 `
--max-duration 1m `
-- opencode --version
Remove the prepared sandbox and workers:
<CodeGroup> ```sh macOS & Linux msb rm -f agent-base coding-worker-1 coding-worker-2 rm -f verify-worker.sh ```msb rm -f agent-base coding-worker-1 coding-worker-2
Remove-Item verify-worker.sh
Remove the reusable snapshot only when you no longer need it:
msb snapshot rm coding-agent-base
Snapshots are immutable. To update packages, recreate agent-base, then overwrite the named snapshot intentionally:
msb snapshot create coding-agent-base --from agent-base --integrity --force
See Snapshots for archive, integrity, and portability details.