Set up a box once, take a snapshot, and start every new box from it in code. System packages, dependencies, repos and files are already there when the box starts.
1. Build the image#
Get a Box API key from the Upstash Console.
Create a box, install what you need, and snapshot it:
Save the printed ID:
A failed command does not throw, so the script checks each exit code before it snapshots. The snapshot does not depend on the box it came from, so the base box can be deleted.
Set it up by hand instead#
The setup does not have to be code. Create a box in the console, connect over SSH, and prepare it like any Linux machine. You can also ask the box’s agent to do it. When the box looks right, snapshot it by ID:
2. Start boxes from it#
Each job gets its own box with everything already installed.
3. Pass settings on restore#
A snapshot holds the disk, the env vars, and the runtime, agent model and browser setting. Everything else is set per box, so pass it to fromSnapshot():
Running processes are not part of a snapshot. Start servers and databases with initCommand, which runs each time the box starts.
4. Update the image#
Restore the current version, change it, and snapshot again:
Point SNAPSHOT_ID at the new ID to roll forward, and back at the old one to roll back. Boxes that are already running keep the version they started from.
Troubleshooting#
apt-getornpm install -gfails with a permission error: a box runs as the non-rootboxuser. Usesudofor system-wide installs.- The server is not running in a restored box: processes do not survive a snapshot. Pass
initCommandon restore. - The network policy is gone after restore: it is not inherited. Pass
networkPolicyagain. - The agent uses a different API key: a key passed as
apiKeyis not stored in the snapshot. Passagentagain on restore.