Conversation
lagmandu
force-pushed
the
feat/configurable-mail-db-port
branch
3 times, most recently
from
September 4, 2026 16:49
11a2711 to
0bb884a
Compare
…fallback Make the PostgreSQL sidecar host port configurable via OPENSHIP_MAIL_DB_PORT to resolve port conflicts when port 5432 is already occupied by a host service. Adds automatic free-port discovery (5433..5460) when the default port is occupied, retains previously assigned ports for existing clusters, and reconciles daemon SQL connection configs on non-default ports. Closes oblien#828
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves #828 by making the mail database (PostgreSQL sidecar) host loopback port configurable via the
OPENSHIP_MAIL_DB_PORTenvironment variable (ordbPortoption), while adding automatic fallback discovery to an available port (5433–5460) when5432is occupied on the host.Context & Problem
Previously, the mail database sidecar hardcoded host port
5432(export const MAIL_DB_PORT = 5432). On servers where an existing PostgreSQL instance or other service is already bound to port 5432, starting the mail stack immediately fails due to a port collision (Bind for 127.0.0.1:5432 failed: port is already allocated), blocking mail installation.Key Changes
Configurable Port & Validation (
packages/adapters/src/infra/mail-container.ts):MAIL_DB_DEFAULT_PORT = 5432,MAIL_DB_FALLBACK_PORT = 5433,MAIL_DB_PORT_RANGE_MAX = 5460, andMAIL_DB_INTERNAL_PORT = 5432.resolveMailDbPort(raw?: string | number)helper that checksprocess.env.OPENSHIP_MAIL_DB_PORT, validates integer boundaries (1..65535), and falls back to5432.MAIL_DB_PORT = resolveMailDbPort()for backwards compatibility.Collision-Free Auto-Discovery (
findAvailableMailDbPortinensure-container-mail.ts):5432is occupied on the host, OpenShip automatically scans5433..5460and selects the first available loopback port, logging the selection without failing.opts.dbPortorOPENSHIP_MAIL_DB_PORT), that exact port is preserved without auto-switching.Cluster Port Retention (
retainedDbPortinensure-container-mail.ts):PG_VERSION), its assigned port is retained fromengine.env, guaranteeing that repairs, restarts, or image swaps never alter the port of a live database.Container Port Mapping & Verification (
ensure-container-mail.ts):buildDbRunCommandmaps-p ${MAIL_DB_HOST_BIND}:${dbPort}:${MAIL_DB_INTERNAL_PORT}so the internal container port remains standard 5432 while the published host loopback port is customizable.startContainerMailandstartDbcheckwaitForPortListening(executor, dbPort).Daemon SQL Config Reconciliation (
apps/email/docker/entrypoint.sh):/etc/postfix,/etc/dovecot,/etc/amavis,/opt/iredapd,/etc/fail2ban) to communicate withOPENSHIP_MAIL_DB_PORTwhen non-default.Public API Exports (
packages/adapters/src/index.ts):resolveMailDbPort,buildDbRunCommand,retainedDbPort,findAvailableMailDbPort, and port constants.Tests:
packages/adapters/src/infra/mail-container.test.ts.packages/adapters/src/system/mail/ensure-container-mail.test.tscoveringbuildDbRunCommand,findAvailableMailDbPortauto-discovery, andretainedDbPort.Verification
5/5).Closes #828