On this page · 10 min
The term ephemeral environments covers short-lived copies of an app, made for one task and deleted afterwards. For a pull request, the task is reviewing one change, so the copy exists from the moment the pull request opens until it closes. This guide covers the three ways to build one, how to choose between them, and how to set up one machine per pull request.
Why shared staging gets in the way
Most teams start with one staging environment that everyone deploys to. Two pull requests reach it at the same time, and the second one overwrites the first. Say pull request A changes a column in a table, and pull request B changes the same table in a different way. Staging gets A's migration, B's tests fail, and nobody can tell whether B's code or A's data is at fault.
A copy per pull request removes the queue. Each change runs on its own copy, so a reviewer sees what that pull request does and nothing else. In the example, each pull request gets its own table, so each test run sees only its own migration. Thoughtworks' definition describes an ephemeral test environment as an isolated, short-lived stack built for one task and easy to destroy afterwards. Long-lived copies drift out of sync with production, so the short life is part of the design. Qovery's guide says these copies usually last hours.
Three ways to build one
Each option gives a pull request its own URL. They differ in what gets copied and how much state survives a push. The machine option comes last in the table and in the paragraphs below because the whole disk, not only a database, carries over from one push to the next.
| Platform previews | Namespace per pull request | Machine per pull request | |
|---|---|---|---|
| What gets copied | The built frontend or app | The services, databases and config | A full Linux machine, with its disk and processes |
| Needs a cluster you run | No | Yes | No |
| State between pushes | Rebuilt on each push | Kept if you keep the database | Kept on the machine's disk |
| Good for | Frontends and static sites | Several services that talk to each other | Long tests, agents, state that must persist |
| Costs you | Less control over the runtime | Cluster upkeep and copying data | Keeping the base snapshot current |
Platform previews suit frontends. A hosting platform builds each branch and serves it at its own address, usually with little setup beyond connecting the repository. The trade-off is control: you run what the platform runs, and an app that needs its own background processes or disk may not fit.
A namespace per pull request deploys the whole stack into a separate section of a Kubernetes cluster, called a namespace, and removes that section when the pull request closes. One vendor write-up on preview environments notes that some organizations run up to 30 microservices in one preview environment. This is the strongest option when your services must talk to each other and you already run Kubernetes. The same article lists data cloning and migrations among the hard parts, because each namespace needs its own data.
A machine per pull request gives each pull request one Linux machine with root access, its own disk and its own HTTPS URL. You start it from a snapshot, which is a saved copy of a machine with its dependencies already installed, so one base serves every pull request. The machine keeps its state between pushes, and you decide what runs on it. boxd is one provider of this approach.
Which one fits your pull requests
Match the option to the situation the pull request is in:
- A frontend change, or an app with no other services to run: use platform previews. It is the least work to set up.
- Several services that must talk to each other, on a cluster you already run: use a namespace per pull request. Plan the data for each copy before you start, since that is where the effort goes.
- Tests that run for hours, a database that must keep its data, or an agent that works inside the environment: use a machine per pull request, on boxd. The steps below use this option.
The machine option has three properties that matter in that situation. It also has a cost: you keep the base snapshot current with your main branch, and you delete machines when their pull requests close.
- Starting a machine from a snapshot takes a median 0.46 s in Frankfurt and 0.73 s in New York. This covers only the start of the machine, not your app's own startup. We measured it on 2026-11-10 with the open-source aisandboxbench harness, which anyone can run.
- Each machine has a 100 GB disk that grows only as data is written, plus its own HTTPS URL. Data from one pull request stays on that pull request's machine. Sizes and limits are in the resource limits guide.
- A machine you forget about can delete itself, because
--delete-aftersets a time for deletion whatever state the machine is in. That deletion takes the machine's data with it, even if the pull request is still open, so set a time longer than your longest review.
A machine per pull request is the wrong choice for a static site, or for a stack your team already runs well in Kubernetes. Machines also don't isolate untrusted code by default: if a pull request comes from a fork, check the isolation options in the command reference before you run its code.
What you'll have, and what you need first
At the end, every open pull request has its own URL. The URL is rebuilt on each push and deleted when the pull request closes.
You need three things:
- The boxd CLI, installed and signed in. The install line is at the end of this guide.
- An app that builds and runs from its repository.
- An open pull request to try it on. The steps below use pull request 42, so replace
42with your number.
How to set up ephemeral environments for one pull request
These steps use boxd, with one machine per pull request. Run them by hand first. Once they work, put them in CI.
1. Save a base snapshot
boxd new app-base
boxd machine exec app-base -- 'git clone <your-repo-url> /home/boxd/app && cd /home/boxd/app && npm ci'
boxd snapshots save app-base app-base-v1The first command creates the base machine. The second copies your repository onto it and installs its dependencies. The third saves the machine's memory and disk as a snapshot named app-base-v1. You should see the snapshot saved while app-base keeps running.
If the app needs a database, install it on app-base and load the seed data you want each pull request to start with. The machine's disk carries that data into the snapshot, and each pull request's machine starts with its own copy.
Treat the preview URL as public: anyone with the link can reach the dev server, which has no login. Keep real secrets out of the snapshot.
Save the snapshot before you start the app. A snapshot includes the processes that are running, so a server started now would show up in every machine made from it.
2. Create the pull request's machine
boxd new pr-42 --from-snapshot app-base-v1 --delete-after 7dThis starts a machine called pr-42 from the snapshot. The --delete-after 7d flag is a safety net: the machine is deleted after seven days even if you forget step 6. You should see the new machine's name in the output, and it gets its own HTTPS URL.
3. Check out the pull request and start the app
boxd machine exec pr-42 -- 'fuser -k 8000/tcp 2>/dev/null || true; cd /home/boxd/app && git fetch origin pull/42/head && git checkout --detach FETCH_HEAD && npm install && nohup npm run dev -- --port 8000 > /home/boxd/app.log 2>&1 &'This fetches the pull request's head commit, checks it out, installs what changed in its dependencies, and starts the dev server, the process that reloads your code as you edit it. The fuser part stops whatever is still listening on port 8000, including the server's child process, so running the command again after a push picks up the new code. Checking out the commit rather than a branch name lets the same command run again on every push. The command returns straight away, and the server writes its output to /home/boxd/app.log.
4. Open the preview
curl -sI https://pr-42.boxd.shYou should see a status line that starts with HTTP/ and ends with 200. If the request hangs, the machine may be asleep. Connecting to it wakes it: boxd connect pr-42 opens a shell and wakes a machine on standby or hibernated. If you see a 502 instead, the server is not up yet. Read its log:
boxd machine exec pr-42 -- 'tail -n 50 /home/boxd/app.log'5. Update it on each push
Run step 3 again with the same pull request number. The machine and its URL don't change; only the code on the machine does.
6. Delete it when the pull request closes
boxd machine remove pr-42 -yThis deletes the machine and its disk for good, so the URL stops working. The -y flag skips the confirmation prompt, which scripts need.
Run it on every pull request
Once the steps work by hand, put them in a workflow that runs on each pull request event. Run steps 2 and 3 when a pull request opens, and step 3 again on each new push. Run step 6 when it closes. Use the pull request number in the machine name, so each pull request has exactly one machine.
This guide doesn't cover how a CI job signs in to boxd, so check the docs before you wire that in. The CI runners page covers the wider picture.
Which option to start with
Start with platform previews when the change is a frontend. Start with a machine per pull request, on boxd, when tests or state are the problem. Rebuild the base snapshot from your main branch on a regular schedule, so the copies don't drift from production. Use a namespace per pull request when services must run together and you already run Kubernetes. Whichever you pick, delete the environment when its pull request closes. A copy that outlives its change drifts from production, and then the preview stops showing what the reviewer needs to see.
Next, run steps 1 to 6 on one open pull request this week. Read the command reference for every flag, and the snapshots guide before you save your first base. The snapshot your app guide shows how to prepare the base.
Install: curl -fsSL https://boxd.sh/downloads/install.sh | sh
Frequently asked
- What are ephemeral environments?
- Short-lived copies of an app, made for one task and deleted afterwards. For a pull request, the copy exists from when the pull request opens until it closes.
- What is a preview deployment?
- A running copy of a branch's code at its own URL, so a reviewer can test the change before it is merged.
- Do I need Kubernetes for preview environments?
- No. Platform previews and a machine per pull request don't need a cluster. A namespace per pull request does, and suits teams that already run Kubernetes.
- How long does it take to start a machine from a snapshot?
- A median 0.46 s in Frankfurt and 0.73 s in New York, measured with the open-source aisandboxbench harness on 2026-11-10.
- How do I delete a preview environment when its pull request closes?
- Run boxd machine remove with the machine's name and -y from your CI job on the pull request's closed event. The -y flag skips the confirmation prompt.



