devsandbox 0.6.0

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
---
title: Private services per instance
description: Every instance gets its own Postgres and Redis, so agents never touch each other's data.
order: 3
icon: network
tags: [services, networking, agents]
related: [shared-postgres, parallel-agents, port-forwarding]
---

import AgentPrompt from '../../components/AgentPrompt.astro';
import AgentPromptLink from '../../components/AgentPromptLink.astro';
import Agent from '../../components/Agent.astro';
import AgentOnly from '../../components/AgentOnly.astro';
import AgentPicker from '../../components/AgentPicker.astro';

## The problem

Parallel agents on one database step on each other: one runs a destructive migration while another's tests expect the old schema, one flushes Redis in the middle of another's run. For throwaway work each instance should own its backing services and be free to wreck them.

That is the default. A service without `scope` is **isolated**: every instance that lists it gets its own container, on a network private to that instance, under the same hostname.

<AgentPromptLink />

## Config

<AgentPicker />

```toml title="devsandboxes.toml" agent="claude"
[services.postgres]
image = "postgres:16"                  # no scope: one per instance
env = { POSTGRES_PASSWORD = "postgres" }

[services.redis]
image = "redis:7"
command = ["redis-server", "--save", "", "--appendonly", "no"]  # in-memory only

[sandbox.api]
folder = "../api"                      # your repo, next to this config dir
image = "node:22"
services = ["postgres", "redis"]
# Your Claude Code login and settings, from the host.
mounts = ["source=${localEnv:HOME}/.claude,target=/root/.claude,type=bind"]

[sandbox.api.containerEnv]  # same hostnames in every instance, its own containers
DATABASE_URL = "postgres://postgres:postgres@postgres:5432/postgres"
REDIS_URL = "redis://redis:6379"
CLAUDE_CONFIG_DIR = "/root/.claude"

[sandbox.api.postCreateCommand]  # once, when an instance is created
agent = "npm install -g @anthropic-ai/claude-code"
```

```toml title="devsandboxes.toml" agent="codex"
[services.postgres]
image = "postgres:16"                  # no scope: one per instance
env = { POSTGRES_PASSWORD = "postgres" }

[services.redis]
image = "redis:7"
command = ["redis-server", "--save", "", "--appendonly", "no"]  # in-memory only

[sandbox.api]
folder = "../api"                      # your repo, next to this config dir
image = "node:22"
services = ["postgres", "redis"]
# Your Codex login and settings, from the host.
mounts = ["source=${localEnv:HOME}/.codex,target=/root/.codex,type=bind"]

[sandbox.api.containerEnv]  # same hostnames in every instance, its own containers
DATABASE_URL = "postgres://postgres:postgres@postgres:5432/postgres"
REDIS_URL = "redis://redis:6379"

[sandbox.api.postCreateCommand]  # once, when an instance is created
agent = "npm install -g @openai/codex"
```

<AgentOnly agent="claude">

On macOS <Agent /> keeps its login in the Keychain: run `/login` once in any instance.

</AgentOnly>

## Try it

One instance per task, each with its own postgres and redis:

```bash
devsandbox run api --name fix-login
devsandbox run api --name add-search
devsandbox stats        # both instances and all four service containers
```

- `--name`: the instance name.

Start <Agent /> in each:

```bash agent="claude"
devsandbox exec -it fix-login claude
devsandbox exec -it add-search claude
```

```bash agent="codex"
devsandbox exec -it fix-login codex
devsandbox exec -it add-search codex
```

`devsandbox rm add-search` removes the instance with its services and network.

## How it works

- Services follow their instance through `stop`, `start` and `rm`.
- `devsandbox service rebuild redis` recreates every running instance's redis in place ([services reference](/docs/config#services-servicesname)).
- No host `ports` on isolated services (the second instance would collide): use `devsandbox port`.

## Agent instructions

<AgentPrompt
  goal="Give every instance of my devsandbox sandbox <sandbox name> its own private Postgres and Redis, so parallel <agent> sessions never share data."
  steps={[
    'Add `[services.postgres]` and `[services.redis]` without a `scope` (isolated), with images matching the versions the repo targets and `POSTGRES_PASSWORD` in `env`. No host `ports`.',
    'List both in the sandbox\'s `services` and set the connection variables the repo reads (e.g. `DATABASE_URL`, `REDIS_URL`) in `containerEnv`, with hosts `postgres` and `redis`.',
    'Install <agent> in `postCreateCommand` and mount its host config dir (its login) into the container.',
  ]}
  verify={[
    'Start two instances with `devsandbox -C <config dir> run <sandbox name> --name <task>` and check `devsandbox -C <config dir> stats` lists a postgres and a redis for each.',
    'Run the migrations in one instance and confirm the other\'s database is untouched; check `devsandbox -C <config dir> exec <task> <agent> --version` works.',
  ]}
/>