Back to Microsandbox

OpenCode

docs/examples/agents/opencode.mdx

0.6.103.3 KB
Original Source

This example installs OpenCode in a microVM and opens its terminal UI in an isolated workspace. Project files are copied into the sandbox rather than bind-mounted, so commands and edits cannot directly change the host checkout.

Run OpenCode

<Steps> <Step title="Start OpenCode">

<Tooltip tip="On microsandbox cloud, create the named volumes first and omit replace-on-create from this command."><span className="msb-badge-limited">Limited on cloud <Icon icon="circle-info" size={11} /></span></Tooltip>

Replace ./my-project with the project directory you want OpenCode to work on:

<CodeGroup> ```sh macOS & Linux msb run -t --name opencode-demo --replace \ --cpus 2 --memory 2G --root-disk 4G \ --copy-dir ./my-project:/workspace \ --workdir /workspace \ --mount-named opencode-config:/root/.config/opencode \ --mount-named opencode-data:/root/.local/share/opencode \ node:24-bookworm-slim -- sh -lc ' apt-get update && apt-get install -y --no-install-recommends ca-certificates git && npm install -g [email protected] && exec opencode ' ```
powershell
msb run -t --name opencode-demo --replace `
  --cpus 2 --memory 2G --root-disk 4G `
  --copy-dir ./my-project:/workspace `
  --workdir /workspace `
  --mount-named opencode-config:/root/.config/opencode `
  --mount-named opencode-data:/root/.local/share/opencode `
  node:24-bookworm-slim -- sh -lc '
    apt-get update &&
    apt-get install -y --no-install-recommends ca-certificates git &&
    npm install -g [email protected] &&
    exec opencode
  '
</CodeGroup>

The first run downloads the package and may take a couple of minutes. OpenCode should render its prompt and offer /connect for provider setup. The two named volumes retain its normal XDG configuration and data directories if you recreate the sandbox.

<Tip> To try the TUI without a project, replace `--copy-dir ./my-project:/workspace` with `--mkdir /workspace`. </Tip> </Step> <Step title="Start another session">

After leaving OpenCode, return to the same sandbox and workspace with:

sh
msb exec -t opencode-demo -- opencode
</Step> <Step title="Verify the installation">

Exit the TUI, then run:

sh
msb exec opencode-demo -- opencode --version

The pinned example prints 1.18.4.

</Step> <Step title="Export changes">

Review the diff inside the sandbox before copying anything back:

sh
msb exec opencode-demo -- sh -lc 'cd /workspace && git status --short && git diff --stat && git diff'

For a Git repository, export a patch instead of replacing your host checkout:

sh
msb exec opencode-demo -- sh -lc 'cd /workspace && git add -N . && git diff --binary > /tmp/opencode.patch'

Copy the patch to the host:

sh
msb cp opencode-demo:/tmp/opencode.patch ./opencode.patch

Check that it applies cleanly before applying it:

sh
git apply --check ./opencode.patch
</Step> <Step title="Clean up">

Remove the sandbox:

sh
msb rm -f opencode-demo

Remove persisted configuration and data only when you no longer need them:

sh
msb volume rm opencode-config opencode-data

Keep the volumes if you want to retain provider connections, sessions, and OpenCode configuration. Never copy an agent's entire home directory back to the host.

</Step> </Steps>

Reference