Give a service a fixed port in every slot
A Port variable gets a free port for each slot, picked by the node when a task takes the slot. By default the number can change from one task to the next. Give the variable a preferred port and the service gets that number whenever it can, so you can bookmark it, put it in a test config or open it through a tunnel.
The node uses the preferred port when it is free on that host and no other slot has it. Otherwise it gives the next free port. Two slots never share a port, so a second slot of the same project on one node gets a different number.
Who can do this: anyone who can edit the project. A Port variable always applies to everyone; it can't be an Only me override.
Set a preferred port
- In the sidebar, select Directory, then the Projects tab.
- Select the project's name to open its settings, then select Variables in its list of sections.
- Select Add variable, or select the name of an existing Port variable to change it.
- Enter the Variable name your stack reads its port from, such as
WEB_PORT. - In Value from, pick Port.
- In Port, pick Preferred port, then type the port, such as
5173. It must be from 1024 to 65535. - Select Create, or Save on an existing variable.
To go back to any free port, pick Auto-assigned per slot in Port and select Save.
A new or changed port reaches each slot the next time a task takes it. A stack already running in a slot keeps its old port until then.
Related
- How worktrees work: what a slot is and when a task takes one.
- Run a project's stack in each slot: the stack commands and Port variables.
- Try a task's change in its slot: open the app a slot is running.
- Add an environment variable: every kind of variable.