Does Canton support port 0 and port files?

For Sandbox, I can specify that it should start on an arbitrary free port by passing --port 0 and then read the actual port from the file I specify via --port-file. This can be quite useful in tests where you don’t want to hardcode a specific port.

Does something similar exist for Canton?

By default, you would specify a port in a config file. Instead of hardcoding the port, you can refer to an environment variable.

canton.participants.my-participant.admin-api.port = ${MY_PORT_FROM_ENV}

You can also pass several config files all specifying the same port. Then, the last specified port will be taken.

// The settings from ports.conf will overwrite those from general.conf
canton -c general.conf -c ports.conf

All of that assumes that I have the specific port already right? I’m looking at test scenarios where I’d like the OS to chose any free port (by specifying port 0) and then get access to the port that is actually chosen.

To clarify, the question is not “How do I set the port?” but “How does it react if I set it to 0?”. Most servers understand port 0 to mean “pick any free one”, but of course you then need some mechanism for the server to communicate back with which one was picked.

At the moment, Canton will not try to automatically pick free ports for you.

We have a default port scheme, which may be very vaguely related to your request.
https://www.canton.io/docs/dev/user-manual/usermanual/static_conf.html#default-ports