Change ray serve port number

1. Severity of the issue: (select one)
None: I’m just curious or want clarification.
Low: Annoying but doesn’t hinder my work.
[ *] Medium: Significantly affects my productivity but can find a workaround.
High: Completely blocks me.

2. Environment:

  • Ray version: ‘2.44.1’
  • Python version: 3.12.9
  • OS: ubuntu
  • Cloud/Infrastructure:
  • Other libs/tools (if relevant): fastapi

3. What happened vs. what you expected:

  • Expected: I want to change the serve.run default port number
  • Actual: cat find the solution

run the following code start a ray server, but port 8000 has used

from app.server import FastAPIDeployment
from ray import serve


if __name__ == '__main__':
    serve.run(FastAPIDeployment.bind(), blocking=True)
(ProxyActor pid=19340)     sock.bind((http_options.host, http_options.port))
(ProxyActor pid=19340) OSError: [Errno 98] Address already in use
(ProxyActor pid=19340) RuntimeError: Failed to bind to address '127.0.0.1:8000'. [repeated 4x across cluster]

can’t find any argument related to port number

@PublicAPI(stability="stable")
def run(
    target: Application,
    blocking: bool = False,
    name: str = SERVE_DEFAULT_APP_NAME,
    route_prefix: Optional[str] = "/",
    logging_config: Optional[Union[Dict, LoggingConfig]] = None,
    _local_testing_mode: bool = False,
) -> DeploymentHandle:
    """Run an application and return a handle to its ingress deployment.

    The application is returned by `Deployment.bind()`. Example:

    .. code-block:: python

        handle = serve.run(MyDeployment.bind())
        ray.get(handle.remote())

    Args:
        target:
            A Serve application returned by `Deployment.bind()`.
        blocking: Whether this call should be blocking. If True, it
            will loop and log status until Ctrl-C'd.
        name: Application name. If not provided, this will be the only
            application running on the cluster (it will delete all others).
        route_prefix: Route prefix for HTTP requests. Defaults to '/'.
            If `None` is passed, the application will not be exposed over HTTP
            (this may be useful if you only want the application to be exposed via
            gRPC or a `DeploymentHandle`).
        logging_config: Application logging config. If provided, the config will
            be applied to all deployments which doesn't have logging config.

    Returns:
        DeploymentHandle: A handle that can be used to call the application.
    """
    handle = _run(
        target=target,
        name=name,
        route_prefix=route_prefix,
        logging_config=logging_config,
        _local_testing_mode=_local_testing_mode,
    )

    if blocking:
        wait_for_interrupt()

    return handle

Hi @bot66 , you can use serve.start to change the http port number.

not working, could you elaborate?

from app.server import FastAPIDeployment
from ray import serve
import ray
from ray.serve import HTTPOptions

if __name__ == '__main__':
    http_option=HTTPOptions(host="0.0.0.0",port=7123)

    serve.start(http_option=http_option)
    # serve.run(FastAPIDeployment.bind(), blocking=True)

(ServeController pid=3611203) RuntimeError: Failed to bind to address '127.0.0.1:8000'. [repeated 146x across cluster]