Self-hosted sandboxes
Run your organization's Grain sandboxes on a Linux machine you operate, keep using Grain for everything else, and serve share links on your own domain.
An organization can run its workspaces' sandboxes on a Linux machine it operates instead of Grain's cloud. Builders change nothing: they use the same desktop app and CLI. An owner connects the machine, then switches where the organization's sandboxes run, and Grain moves every workspace there. Everything else (history, comments, share links, the desktop app) still goes through Grain.
What runs where
| On your machine | In Grain's cloud |
|---|---|
| Every workspace's sandbox, once you switch: its files, processes, terminal and preview | Your account, organization and permissions |
| E2B's open-source runtime, installed by Grain's self-hosting kit | Saved content, history, checkpoints, comments and share settings |
| A certificate for your sandbox domain | The share proxy, which checks a link's access and serves it from your machine |
Grain's API reaches your machine over one HTTPS port from one fixed outbound address, 34.45.105.196, which you allowlist. Builders' apps reach their sandboxes directly at <port>-<sandbox>.<your domain>, each protected by its own token. Grain's cloud still stores and passes content, so self-hosting moves where code runs, not where everything is kept. Grain signs a BAA covering what it holds; ask your Grain contact.
Machine requirements
- Linux with KVM: bare metal, or a VM with nested virtualization. On AWS that means a metal instance or M7i, C7i or R7i with nested virtualization. On Google Cloud, an N2 machine with nested virtualization.
- 16 GiB of memory or more. Each running sandbox takes 4 GiB, and the kit keeps room to resume a paused one. A 16 GiB machine runs one sandbox at a time. Larger machines run about one more per 4 GiB.
- Docker Engine 27 or later with the Compose plugin.
- A domain you control for sandboxes, for example
sandbox.example.com, with DNS at Cloudflare, Route 53 or Google Cloud DNS, plus a token that can edit its records. - Inbound port 443 open to the internet. The kit's firewall closes everything else.
Install the kit
Grain shares the self-hosting kit and the template build step with your team when you start. On the machine:
-
Write your DNS provider's token to
/opt/grain-e2b/grain-kit/dns.env(mode 0600), for exampleCF_API_TOKEN=...for Cloudflare. It never leaves the machine. -
Run the installer with your domain, Grain's outbound address (
34.45.105.196) and your own SSH range:sudo ./install.sh --domain sandbox.example.com \ --control-allow 34.45.105.196/32 \ --dns-provider cloudflare --acme-email ops@example.com \ --ssh-allow 203.0.113.0/24 -
Point
api.sandbox.example.comand*.sandbox.example.comat the machine's public address. The kit gets a wildcard certificate for them by DNS challenge. -
Build Grain's three templates (
empty,nextjs,react-vite) on the machine with the template build step Grain provided.
The installer prints the runtime's team API key at the end. Keep it for the next step. Running the installer again applies new flags and updates in place.
Connect your organization
An organization owner runs, from any computer with the Grain CLI:
printf %s "$RUNTIME_KEY" | grain org runtime connect \
--domain sandbox.example.com --share-domain share.example.com --api-key-stdinGrain checks https://api.sandbox.example.com with the key and confirms the three templates are there. It saves nothing if any check fails. Connecting only makes the machine available: nothing moves, and new workspaces keep running on Grain's cloud until an owner switches (next section). Organizations that connected a machine before switching existed already have the setting on their machine.
grain org runtime status shows where the organization's sandboxes run and, for the machine, the domain, the last four characters of the key, the share domain, the templates and when Grain last reached it. Any member can run it. Only owners can connect or disconnect: everyone else gets "Only organization owners can change where sandboxes run." The key is stored encrypted and never shown again.
Switching where sandboxes run
Where your organization's sandboxes run is one setting: Grain's cloud or your machine. Owners change it in the desktop app (organization settings, Sandboxes) or with the CLI:
grain org runtime use self-hostedThe CLI says what will happen and asks before it starts; pass --yes to answer for a script.
Grain then moves every app workspace, a few at a time. For each one it saves a snapshot to history, copies the whole workspace disk (files, data.db, git history, installed packages) to a new sandbox on the other host, checks every file arrived identical and the preview starts, then switches the workspace over in one step. The workspace keeps its ID, so Grain links, share links and CLI checkouts keep working. While its own move runs, a workspace is read-only for a few minutes; open desktop apps show that it is moving and reconnect by themselves, and grain start pauses its sync and resumes on the new host. grain push and grain exec on a moving workspace fail with "Try again in 30 seconds". Its running programs (the app, its dev server, terminals) are paused from the start of its move, so nothing they write is left behind, and start again on the new host; if the move fails, they carry on where they were. HTML workspaces have no sandbox and don't move. New workspaces follow the setting from the moment you change it.
If one workspace fails, it stays where it was, editable, with the reason and a Retry, and the rest carry on. grain org runtime status shows progress, each workspace's steps and errors, the workspaces not moved yet, and earlier switches with who started them; --watch follows it until it finishes. grain org runtime history lists every switch, and grain org runtime history <switch id> shows each workspace's steps and errors in any past switch. The desktop's Sandboxes settings show the last switch only. Any member can read it. An owner retries a failed workspace with grain org runtime retry <workspace> (or every failed one with grain org runtime retry).
Each moved workspace's old sandbox stays paused as an undo until an owner chooses:
- Keep (
grain org runtime keep) deletes the old sandboxes. - Undo (
grain org runtime undo) puts every moved workspace back on its old sandbox and sets the setting back. Changes made since the move are lost. After the 7 days, Undo is refused. If a workspace's old sandbox no longer exists, that workspace stays where it runs now and the switch says so. Undo works within the 7 days even when moving to Grain's cloud is turned off: an owner is warned that the workspaces' data goes back to Grain's cloud and confirms, and moving to Grain's cloud stays off.
If nobody chooses, the old sandboxes are deleted after 7 days. Another switch can't start until then. Stop (grain org runtime stop) leaves the workspaces still waiting where they are, shown as "not moved yet"; running the same switch again moves just those.
Moving workspaces off your machine to Grain's cloud is off by default, so regulated data stays on your machine unless an owner opts in: grain org runtime allow-cloud on.
When the machine is down
Creating or opening a workspace fails within 30 seconds with:
Your organization's sandbox machine (sandbox.example.com) isn't reachable. Workspaces in this organization run only on that machine, so nothing was started anywhere else. Ask an organization owner to check it.
Grain never starts your organization's workspaces on its own cloud instead. grain org runtime status shows the last failure. Once the machine is back, opening the workspace again resumes it there.
grain org runtime disconnect removes the machine. It is refused while workspaces run on the machine or the setting is the machine: switch to Grain's cloud first (grain org runtime use cloud, with moves to the cloud allowed), then disconnect. It is always refused during a switch. If the machine is gone for good, grain org runtime disconnect --force disconnects anyway; its workspaces then show it as unreachable, and Grain won't connect any machine to the organization again until they are deleted. To use a new key or share domain, connect the same domain again without disconnecting. To replace the machine, switch to Grain's cloud, disconnect, connect the new one, then switch to it.
Share links on your domain
With --share-domain share.example.com, every share link for your organization uses https://<link>.share.example.com. Links made earlier on Grain's share host redirect there, and the same link under another organization's domain doesn't exist. Share requests still go to Grain's share proxy, which checks public, password and signed-in access as usual and serves the app from your machine.
To set it up, add two records Grain sends you: *.share.example.com pointing at Grain's share address, and a CNAME that lets Grain issue the certificate for that wildcard. Until the certificate is active, browsers refuse the links. Without a share domain, your links stay on Grain's share host.