Ktor server port precedence between application.conf and PORT env var
0 reputation · 17 Jan 2021, 18:03 UTC
Diagnosing a Ktor deployment that fails health checks often requires knowing which port the embedded server actually bound at startup. Configuration is loaded from application.conf HOCON, environment variables, and command-line arguments, with programmatic configure blocks also available via embeddedServer factories.
The documented merging of these sources is ambiguous. It is unclear when a plain port = 8080 in HOCON is overridden by a PORT environment variable, and whether ${?PORT} substitution is required for env var fallback to work. ApplicationEngineEnvironment merges sources, but the merging strategy for nested objects is not formally specified.
Different server engines may log the bound address at different lifecycle stages, and structured logging formats may omit the effective port unless explicitly added to the log context.
Which configuration source takes precedence for the server port in Ktor 2.x and 3.x when both application.conf and an environment variable are present? Does the embeddedServer configure block always override HOCON and env vars, and is the effective port reliably emitted in startup logs? What is the intended precedence model for containerized deployments?