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):
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 (default127.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.1is reachable and nothing is published. --servicegoes 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
sshper forward, reconnecting if it drops; its ownforwardPortsaren’t copied here. - Unix hosts, TCP only.
Source: site/src/content/examples/port-forwarding.mdxEdit this page on GitHub(opens in a new tab)