EXAMPLE 06
  • networking
  • services

Port forwarding on demand

Open a sandbox's dev server or database from your host, without restarting anything.


The problem

An agent started a dev server on port 3000 inside instance api and you want it in your browser, or psql on your host against that instance’s Postgres. Publishing ports with -p has to happen at create time, collides as soon as a second instance wants the same port, and can’t reach a server listening on the container’s 127.0.0.1.

devsandbox port forwards a port on demand, like VS Code’s Ports tab: it is on localhost until you stop it.

Try it

With an instance api running, from a host terminal (no config needed; runs until Ctrl-C):

TERMINAL
devsandbox port api 3000                        # http://localhost:3000
devsandbox port api 8080:3000                   # host :8080 -> api's :3000
devsandbox port api --service postgres 5432     # a service api uses
devsandbox port my-server/web 3000              # an instance on another machine
  • --service: forward a service’s port instead of the instance’s.
  • --address: the host address to bind (default 127.0.0.1).
  • --machine: a global service on a machine (--machine my-server --service redis 6379); the port still opens here.

From the dashboard: p on an instance or service, type the port; live forwards are on the Ports tab (3). The host daemon keeps them, so they last after you close the dashboard and come back on the same host port, until d stops them; devsandbox port forwards only while it runs. To forward a port whenever the instance runs, add forwardPorts = [3000] to the sandbox (forwardPorts).

How it works

  • Each connection is tunnelled through the in-container helper, so a server on the container’s 127.0.0.1 is reachable and nothing is published.
  • --service goes through a running instance that uses the service (services).
  • A bare port taken on the host falls back to the next free one; the printed line says which.
  • On a machine, the same tunnel runs over one ssh per forward, reconnecting if it drops; its own forwardPorts aren’t copied here.
  • Unix hosts, TCP only.

Source: site/src/content/examples/port-forwarding.mdxEdit this page on GitHub(opens in a new tab)

MENU /
View on GitHub
EXAMPLES
All examplesEvery recipe on one page.Parallel coding agentsSeveral coding agents on one repo, fully isolated from one another.One shared PostgresOne development database for all your sandboxes and their instances.Private services per instanceEvery instance gets its own Postgres and Redis, so agents never touch each other's data.Shared package cachesNew sandboxes stop downloading the same packages again.ssh-agent and git pushAgents push over ssh with your host's keys, which never enter the container.Port forwarding on demandOpen a sandbox's dev server or database from your host, without restarting anything.Attach VS CodeOpen any instance in VS Code with one key.devcontainer FeaturesAdd Node, Rust or the GitHub CLI to any image, in TOML.Babysit pull requestsAn agent keeps your open PRs merged with their base, and pings you when it can't.Port a devcontainer.jsonTurn an existing devcontainer.json into a devsandbox config.