# Port and socket forwarding

> For the complete documentation index, see [llms.txt](https://docs-stg.ringleader.dev/llms.txt).


> A forward makes a service that listens on a workstation reachable at an address on your computer.


A forward makes a service that listens on a workstation reachable at an address on your
computer. You can then open `http://127.0.0.1:8080` in your browser as if the service ran
there. Forwards also carry Unix sockets, such as the workstation's Docker socket. Ringleader
sets up forwards from a workstation's configuration, and you can write your own.

## Forwards as LocalBindings

A LocalBinding is a file that describes what this computer forwards from a workstation. It
forwards TCP ports and Unix sockets, either listed or found when the workstation starts
listening. Each computer owns and writes its own LocalBindings. A LocalBinding never leaves the computer and
is never synced, so two computers forward on their own. A LocalBinding can also carry a
service on your computer into a workstation, as
[Services on your computer](https://docs-stg.ringleader.dev/workstations/connectivity/computer-services/)
explains.

[LocalBinding](https://docs-stg.ringleader.dev/reference/resources/localbinding/) lists the fields. To see what a
computer forwards now, which host ports are in use and any conflicts, run `rl binding show`.

## Default forwards from a workstation's configuration

Most of the time you don't write a LocalBinding. A workstation's configuration declares the
ports it listens on with `ports`, and a `defaultLocalBinding` template that says how to
forward them. Ringleader creates a LocalBinding from the template on your computer once. It
does so when it sees the workstation `Running` and has no record of a LocalBinding for it. A workstation that already has its LocalBinding keeps it, and if you delete that LocalBinding,
Ringleader doesn't create it again.

[Ports and forwarding](https://docs-stg.ringleader.dev/reference/resources/workstationconfig/#ports-and-forwarding) lists
the template's fields. The template can't declare a service on your computer, because only
the computer's owner decides which of its services a workstation may reach.

## Forwards on a shared workstation

A workstation shared with you gives you SSH access, and nothing is forwarded to your computer
by default. The owner opts in by setting `defaultLocalBinding.scope: accessors` in the
workstation's configuration. Even then, the LocalBinding that your computer creates starts
disabled. Nothing is forwarded until you enable it, because a forward puts a listener on your
computer. To enable it, run `rl binding enable <workstation> -n <namespace>`, or press `a` on
the Bindings tab of the terminal UI.

Until the owner sets the scope, a LocalBinding on your computer that matches only shared
workstations reports the reason `SharedWorkstation` in its status.

## Host ports and conflicts

A forward uses the same port number on your computer as on the workstation, unless you change
it:

- `localPort` on one port sets the port on your computer, and `localPort: 0` lets Ringleader
  choose a free one.
- `portOffset` shifts the port of every automatic forward that has no `localPort`. With `10000`, workstation
  port 8080 appears on your computer as 18080.

When two forwards want the same port on your computer, one of them gets it. What happens to
the other depends on how it asked for the port:

- A forward that uses the workstation's port number, or the number `portOffset` gives it, moves
  to another free port. If two workstations both listen on 8080, one forward uses 8080 and the
  other uses 18080.
- A forward whose `localPort` you set stays on that port. If the port is taken, Ringleader
  doesn't forward it and records it in `status.dropped` with the reason.

To choose whether a forward can move, set `onPortConflict` for a whole LocalBinding or for one
port. `reassign` lets the forward move, and `drop` keeps it on its port. A forward with
`localPort: 0` always gets a port that Ringleader chooses.

If you set `localPort` to a port number and the forward can't move, Ringleader reserves that port
for it, even while its workstation is stopped. When another workstation starts and its forward
wants the same port but can move, that forward goes to another free port. Your forward is still
dropped if another forward already uses the port or has saved it, or if another program on your
computer holds it.

`rl binding show` lists the port each forward uses, and adds `(wanted host:8080)` to a forward
that moved off the port it asked for.
[Conflict handling](https://docs-stg.ringleader.dev/reference/resources/localbinding/#conflict-handling) has the
order in which forwards get their ports.

## Saved host ports

Ringleader remembers the port it gave a forward, so an address you bookmarked keeps working.
It saves the port for each forward that can move and that names its port: an entry in `ports`,
an entry in `autoForward.ports`, or a port the workstation's configuration declares. It saves
the port even when the forward got the one it asked for. Ringleader keeps the saved port on your
computer. It lasts through reconnects, a stopped workstation, and restarts of the workstation or
the Ringleader daemon. A saved port has priority over a new forward that wants the same number.

A port that `forwardAll` found and nothing names isn't saved. Its forward keeps the same port on
your computer only while the workstation stays connected and keeps listening on that port.

If another program on your computer takes a saved port, the forward shows as dropped. Ringleader
tries the port again about 30 seconds later. If the program still holds it, Ringleader picks a
new port and saves that one instead.

Ringleader forgets a saved port when you remove or disable the LocalBinding or the port's
entry, when the LocalBinding no longer selects the workstation, when you change the port the
entry asks for, or when the workstation is deleted.

## Service listen address

A forward connects to `127.0.0.1` inside the workstation, so the service must accept
connections there. Start a development server with its host set to `127.0.0.1`, as Vite does
with `npm run dev -- --host 127.0.0.1`.

A port that a devcontainer publishes isn't forwarded to your computer on its own, including
the ports in `forwardPorts`, as
[Devcontainer ports](https://docs-stg.ringleader.dev/workstations/packages-tools-settings/devcontainers/#devcontainer-ports)
explains.

## Forward one port

To forward one port, name it in a LocalBinding. A per-port entry opts that port in on its own,
so you don't need `forwardAll`:

```yaml
apiVersion: core.ringleader.dev/v1
kind: LocalBinding
metadata:
  name: web-port
  namespace: local
spec:
  workstationSelector:
    matchName: my-box
  autoForward:
    ports:
      - port: 8080          # the port on the workstation
        localPort: 18080    # the port on your computer
```

Apply the file with `rl apply -f`. Open `http://127.0.0.1:18080` on your computer to reach
port 8080 on the workstation.

## Forward every listening port

To forward every port a workstation listens on, set `forwardAll`. This example keeps one port
off and moves another:

```yaml
apiVersion: core.ringleader.dev/v1
kind: LocalBinding
metadata:
  name: web-ports
  namespace: local
spec:
  workstationSelector:
    matchLabels:
      app: web
  autoForward:
    forwardAll: true
    ports:
      - port: 9999
        enabled: false      # leave this port out
      - port: 80
        localPort: 8080     # move this port on your computer
```

A binding that selects several workstations often finds the same port on more than one of
them. Those forwards move to free ports, as
[Host ports and conflicts](#host-ports-and-conflicts) explains. To choose every port yourself,
write one LocalBinding for each workstation and give each port a `localPort`.

## Forward a workstation's Docker socket

To reach a workstation's Docker daemon from your computer, forward its Docker socket to a path
on your computer:

```yaml
apiVersion: core.ringleader.dev/v1
kind: LocalBinding
metadata:
  name: docker
  namespace: local
spec:
  workstationSelector:
    matchName: my-box
  sockets:
    - remotePath: /var/run/docker.sock
      localPath: /tmp/ringleader-docker.sock
```

To forward the Docker socket of every workstation a configuration reaches, use
`defaultLocalBinding.dockerSocket` instead. Ringleader then picks the path on each computer,
as [Ports and forwarding](https://docs-stg.ringleader.dev/reference/resources/workstationconfig/#ports-and-forwarding)
shows.

## Turn a forward on or off

To turn a LocalBinding off without deleting it, run `rl binding disable` with its name.
`rl binding enable` turns it on again, and `-l` takes a label selector for several at once:

```bash
rl binding disable web-ports -n local
rl binding enable web-ports -n local
```

In the terminal UI, press `a` on the Bindings tab.
[Local bindings](https://docs-stg.ringleader.dev/reference/cli/commands/bindings/) lists the flags and the commands that
retarget a binding at another workstation.

## Forward the ports of a shared workstation

To let the people you share a workstation with forward its ports, set the scope in its
configuration:

```yaml
spec:
  defaultLocalBinding:
    enabled: true
    scope: accessors
    autoForward:
      forwardAll: true
```

Each person then enables the LocalBinding that their computer creates, as
[Forwards on a shared workstation](#forwards-on-a-shared-workstation) explains.

## Next steps

- [LocalBinding](https://docs-stg.ringleader.dev/reference/resources/localbinding/) lists every field of a LocalBinding
  and what its status reports.
- [Local bindings](https://docs-stg.ringleader.dev/reference/cli/commands/bindings/) covers the `rl binding` commands.
- [Services on your computer](https://docs-stg.ringleader.dev/workstations/connectivity/computer-services/)
  carries a service on your computer into a workstation, the opposite direction.
- [Diagnose a workstation](https://docs-stg.ringleader.dev/troubleshooting/#diagnose-a-workstation) shows how
  `rl troubleshoot` reports which LocalBinding points at a workstation and what it forwards.

