Skip to content
Back to skills

Local Stack

ASecurity

Run an isolated per-worktree LLM Gateway stack — own Postgres, Redis, storage Redis, and app ports — and tear it down safely. Use before running unit tests, starting dev servers, reseeding, or running docker compose in a worktree; when choosing STACK_SUFFIX, port slots, or .envrc; when a port is taken, a local server behaves like another worktree's code, or a relocated frontend fails CORS or login; when asked to clean up, close everything, or stop ports and docker.

  • 1,773 stars
  • 0 votes
  • 0 copies
  • 5 views
  • Added September 29, 2026
ai-agentsrustshellbashnextjsnodedockergitapidatabasefrontend

Works with

  • api

Security analysis

A92/100
  • mediumInstalls packages at runtime which could introduce malicious dependencies

Pro shows the line behind each finding and how to fix it

Scanned December 5, 2026

npx -y skills add theopenco/llmgateway --skill local-stack --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Local Stack?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Local Stack
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/theopenco-local-stack/badge)](https://www.skillsdirectory.com/skills/theopenco-local-stack)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: local-stack
description: Run an isolated per-worktree LLM Gateway stack — own Postgres, Redis, storage Redis, and app ports — and tear it down safely. Use before running unit tests, starting dev servers, reseeding, or running docker compose in a worktree; when choosing STACK_SUFFIX, port slots, or .envrc; when a port is taken, a local server behaves like another worktree's code, or a relocated frontend fails CORS or login; when asked to clean up, close everything, or stop ports and docker.
---

# Local stack per worktree

Several worktrees share this machine, and only one stack can own the default
ports (:4001, :5432, :6379). Before assuming local edits are live, confirm which
tree serves a port: `lsof -a -p <pid> -d cwd -Fn`. Give each worktree its own
stack instead of sharing the defaults.

Every host-facing port and Docker name is env-var driven; the defaults reproduce
the single shared stack.

## Pick a slot

Pick a slot _N_ (1, 2, 3 …) and offset every port:

| Component            | Env var              | Default | Slot _N_        | Slot 1 |
| -------------------- | -------------------- | ------- | --------------- | ------ |
| Postgres (host port) | `POSTGRES_PORT`      | 5432    | 5432 + N × 100  | 5532   |
| Redis                | `REDIS_PORT`         | 6379    | 6379 + N × 1000 | 7379   |
| Storage Redis        | `STORAGE_REDIS_PORT` | 6479    | 6479 + N × 1000 | 7479   |
| Gateway              | `GATEWAY_PORT`       | 4001    | 4001 + N × 100  | 4101   |
| Gateway metrics      | `METRICS_PORT`       | 9090    | gateway only    | 9190   |
| API                  | `API_PORT`           | 4002    | 4002 + N × 100  | 4102   |
| UI                   | `UI_PORT`            | 3002    | 3002 + N × 100  | 3102   |
| Playground           | `PLAYGROUND_PORT`    | 3003    | 3003 + N × 100  | 3103   |
| Code                 | `CODE_PORT`          | 3004    | 3004 + N × 100  | 3104   |
| Docs                 | `DOCS_PORT`          | 3005    | 3005 + N × 100  | 3105   |
| Admin                | `ADMIN_PORT`         | 3006    | 3006 + N × 100  | 3106   |
| Airside              | `AIRSIDE_PORT`       | 3007    | 3007 + N × 100  | 3107   |

Redis uses ×1000 so slot 1's Redis does not land on another worktree's default
storage-Redis port (6479).

## Configure `.envrc`

Put the block in the worktree's gitignored `.envrc` (direnv; `direnv allow`
after editing). Exported vars reach `docker compose`, `drizzle-kit`, `vitest`,
and Node, and Node's `--env-file` never overrides an exported var.

```bash
# --- isolated stack: worktree "tel-aviv", slot 1 ---
export STACK_SUFFIX=-tel-aviv      # container name suffix (include the separator)
export COMPOSE_PROJECT_NAME=llmgateway${STACK_SUFFIX}   # required: Compose v2.7 ignores ${STACK_SUFFIX} in name:
export POSTGRES_PORT=5532
export REDIS_PORT=7379
export STORAGE_REDIS_PORT=7479

export DATABASE_URL=postgres://postgres:pw@localhost:5532/db
export TEST_DATABASE_URL=postgres://postgres:pw@localhost:5532/test

export GATEWAY_PORT=4101
export API_PORT=4102
export UI_PORT=3102
export PLAYGROUND_PORT=3103
export CODE_PORT=3104
export DOCS_PORT=3105
export ADMIN_PORT=3106
export AIRSIDE_PORT=3107

# URLs the services hand to each other / render into pages
export API_URL=http://localhost:4102
export UI_URL=http://localhost:3102
export APP_URL=http://localhost:3102
export GATEWAY_URL=http://localhost:4101
export PLAYGROUND_URL=http://localhost:3103
export DOCS_URL=http://localhost:3105
export ADMIN_URL=http://localhost:3106
export AIRSIDE_URL=http://localhost:3107
export ORIGIN_URLS=http://localhost:3102,http://localhost:3103,http://localhost:3104,http://localhost:3105,http://localhost:3106,http://localhost:3107,http://localhost:4102
```

Agent shells do not run direnv. Prefix every command that depends on the block
with `set -a && source .envrc && set +a`.

## Start

In a fresh worktree run `pnpm install` first, with the pinned pnpm
(`packageManager` in `package.json`).

```bash
docker compose -p "llmgateway${STACK_SUFFIX}" ps      # must list only your containers, or none
docker compose -p "llmgateway${STACK_SUFFIX}" up -d   # containers postgres-tel-aviv, …
pnpm wait-for-services
pnpm push-test     # test database, for pnpm test:unit
pnpm push-dev      # dev database, for dev servers
pnpm seed          # dev database
```

`pnpm setup` (and `pnpm setup:db`) runs a bare `docker compose down -v` first,
deleting every Postgres and Redis volume in the compose project. Run it only
with `COMPOSE_PROJECT_NAME` exported for your own stack.

Before any `push-*`, `seed` or test command, confirm your containers own the
ports. If `docker ps` shows a port held by a container whose name lacks your
`STACK_SUFFIX`, move to another slot; never stop or reuse that container.
A `push-dev` or `seed` against another worktree's Postgres silently destroys
its data.

If `up` fails with `all predefined address pools have been fully subnetted`,
pin this worktree's subnet instead of pruning networks other worktrees use:

```yaml
# docker-compose-override-<worktree>.yml (gitignored)
networks:
  default:
    ipam:
      config:
        - subnet: 10.99.<slot>.0/24
```

Pass `-f docker-compose.yml -f docker-compose-override-<worktree>.yml` on every
compose command for the worktree, including `down`.

Turbo's strict env mode drops the undeclared port and `DATABASE_URL` vars, so
start dev servers with `pnpm exec turbo run dev --env-mode=loose` and confirm
the startup logs show your ports. To relocate one service, run it directly:

```bash
( cd apps/api && API_PORT=4102 API_URL=http://localhost:4102 UI_URL=http://localhost:3102 \
    ORIGIN_URLS=http://localhost:3102,http://localhost:4102 \
    node --enable-source-maps --env-file=../../.env dist/serve.js )   # build first: turbo run build --filter=api
```

`dist/serve.js` has no watch; rebuild and restart after changes. The worker runs
`dist/index.js`. Stop servers by PID (`lsof -nP -iTCP:<port> -sTCP:LISTEN`); a
`pkill -f` pattern also kills other worktrees' servers.

## How the wiring works

- **Docker**: each `container_name` interpolates `STACK_SUFFIX`, but the top-level `name: llmgateway${STACK_SUFFIX:-}` does not on Compose v2.7 (`docker compose version`): `docker compose config` prints `name: llmgatewaystack_suffix-`, so every stack started without `-p` lands in that one project and a bare `down -v` wipes them all. Always pass `-p "llmgateway${STACK_SUFFIX}"` or export `COMPOSE_PROJECT_NAME`. The default shared stack is project `llmgateway`. Address containers with `docker compose -p … exec postgres …`, not `docker exec postgres …`.
- **Databases**: apps read `DATABASE_URL`; tests read `TEST_DATABASE_URL`, falling back to `DATABASE_URL`, then `postgres://postgres:pw@localhost:5432/test`. Set both: `TEST_DATABASE_URL` keeps `pnpm test:unit` off the dev database, and `pnpm push-test` pushes to it.
- **Redis**: `packages/cache` reads `REDIS_HOST`/`REDIS_PORT` and `STORAGE_REDIS_HOST`/`STORAGE_REDIS_PORT`. Any `STORAGE_REDIS_*` var enables the separate storage instance, so always set `STORAGE_REDIS_PORT`.
- **Service ports**: gateway and api fall back to `PORT`; use `GATEWAY_PORT` and `API_PORT` instead and leave `PORT` unset. Set `METRICS_PORT` only on the gateway process (it defaults to 9090, so a second gateway needs it); api and worker start their own metrics server whenever it is set. The Next.js `dev` scripts read `UI_PORT`/`PLAYGROUND_PORT`/`CODE_PORT`/`DOCS_PORT`/`ADMIN_PORT`.
- **Auth + CORS**: the API reads `ORIGIN_URLS` (CORS and better-auth trusted origins) and `UI_URL`. Add every relocated frontend's origin to `ORIGIN_URLS`. The session cookie is host-only for `localhost`, so login works across ports.
- **Frontends → backends**: frontends read `API_URL` and `GATEWAY_URL` server-side in `apps/*/src/lib/config-server.ts`.

## Tear down

Tear down a stack you started in this session once the work is finished:

```bash
scripts/dev-reset.sh --dry-run    # then without --dry-run
```

It stops only this worktree's dev servers and runs `docker compose -p
"llmgateway${STACK_SUFFIX}" down`, refusing when that project's containers were
started from another worktree. `--volumes` adds `-v`, `--worktrees` removes
clean, idle worktrees with a merged PR, `--main` checks out and pulls main
(`--help`).

Leave the default shared stack (no `STACK_SUFFIX`, project `llmgateway`) and
any stack you did not start running: it is the user's development database,
and `-v` destroys its volumes. The script refuses it without `--default`.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.