Bake cross-service port collision avoidance into every service script

With 70+ services sharing a handful of common default ports (emby and
jellyfin both default to 8096, changedetection and frigate both default
to 5000, arm and nextcloud both default to 8080...), nothing previously
checked whether a service's default port was actually free on the host.
Whichever service installed second would silently write a compose file
claiming an already-held port, only failing at `docker compose up` time.

Adds two shared helpers to lib/common.sh:
- port_in_use PORT [PROTO] — true if something's already listening
- find_free_port VARNAME START [PROTO] — scans upward, writes back the
  first free port

Every service that publishes a fixed host port now scans before writing
docker-compose.yml, on every install (not just when adding an explicit
additional instance). On a normal single-install host this is a silent
no-op; it only changes behavior when something else already holds the
port.

- The 19 services already given multi-instance support this session had
  their port scan moved out of the "add instance" branch to run
  unconditionally, since the same collision risk exists on a plain first
  install.
- 20 more services with previously-hardcoded ports gained scanning for
  the first time: archivebox, arm, calibre-web, changedetection,
  drum-rhythm-game, gatus, n8n, nextcloud, onlyoffice, stirling-pdf,
  uptimekuma, portainer, iopaint (both GPU/CPU compose branches), koha
  (paired), syncthing (paired), wg-easy (paired, plus WG_PORT env so
  generated peer configs keep the right Endpoint), homeassistant
  (bridge-mode only — host mode can only warn), frigate and
  frigate-audio (multi-port stacks, moved together).
- caddy.sh is the deliberate exception: 80/443 stay fixed and only warn
  on collision, since silently moving Caddy itself would leave nothing
  listening where any client actually looks.
- authelia.sh needs no change — it has no published host port at all.
- Every service's standalone bootstrap fallback (sudo bash services/x.sh
  with no sibling files) got the same two helpers duplicated into its
  stub block, matching how every other shared helper is already handled
  there.

Documents the full pattern in CLAUDE.md's new "Port collision avoidance"
section, including the quoted-heredoc/backtick-escaping gotcha and the
network_mode:host limitation (can only scan ports the app takes as a
configurable env var).

Verified via bash -n on every changed file, plus functional runs seeding
occupied ports for each collision shape used here (single, paired,
multi-port stacks) and confirming the scan/shift and generated
compose/README output are correct — including the emby/jellyfin,
nextcloud/arm, and frigate/changedetection collision scenarios that
originally motivated this.
This commit is contained in:
Claude
2026-08-09 23:55:49 +00:00
parent b860a8b174
commit 9e06ed4b83
42 changed files with 1269 additions and 254 deletions
+44 -9
View File
@@ -41,6 +41,21 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
chown -R "$ACTUAL_USER:$ACTUAL_USER" "$@" 2>/dev/null || true
}
port_in_use() {
local _port="$1" _proto="${2:-tcp}"
local _flag="-tlnH"
[ "$_proto" = "udp" ] && _flag="-ulnH"
ss "$_flag" "sport = :${_port}" 2>/dev/null | grep -q .
}
find_free_port() {
local _varname="$1" _port="$2" _proto="${3:-tcp}"
while port_in_use "$_port" "$_proto"; do
_port=$((_port + 1))
done
eval "$_varname='$_port'"
}
# Match common.sh's eval-based pattern so local vars in install_* are set correctly
prompt_text() {
local _q="$1" _def="$2" _var="$3" _r
@@ -157,13 +172,28 @@ install_syncthing() {
log_info "Installing Syncthing..."
local DIR="$DOCKER_DIR/syncthing"
local WEB_PORT="8384" SYNC_PORT="22000" DISCOVERY_PORT="21027"
if [ "$DRY_RUN" = true ]; then
echo "[DRY-RUN] Would create $DIR with docker-compose.yml and .env"
echo "[DRY-RUN] Would expose web UI on 8384, sync protocol on 22000 (tcp+udp), discovery on 21027/udp"
echo "[DRY-RUN] All 3 auto-scanned/shifted together if occupied"
return 0
fi
# Scan for free host ports, moving all 3 together — a plain install
# shouldn't silently claim a port another already-running service holds.
# Note: DISCOVERY_PORT (LAN broadcast) only works for zero-config local
# discovery at its standard number — shifting it away from 21027 means
# peers must be added manually by device ID instead of relying on
# auto-discovery. See CLAUDE.md's "Port collision avoidance" section.
while port_in_use "$WEB_PORT" || port_in_use "$SYNC_PORT" \
|| port_in_use "$SYNC_PORT" udp || port_in_use "$DISCOVERY_PORT" udp; do
WEB_PORT=$((WEB_PORT + 1))
SYNC_PORT=$((SYNC_PORT + 1))
DISCOVERY_PORT=$((DISCOVERY_PORT + 1))
done
mkdir -p "$DIR"
ensure_docker_dir_ownership "$DIR"
cd "$DIR" || return 1
@@ -209,10 +239,10 @@ services:
- PGID=\${PGID:-$PGID}
- TZ=\${SITE_TZ:-UTC}
ports:
- "8384:8384"
- "22000:22000/tcp"
- "22000:22000/udp"
- "21027:21027/udp"
- "${WEB_PORT}:8384"
- "${SYNC_PORT}:22000/tcp"
- "${SYNC_PORT}:22000/udp"
- "${DISCOVERY_PORT}:21027/udp"
volumes:
- ./config:/var/syncthing/config
- ./data:/var/syncthing
@@ -237,15 +267,20 @@ ENV
Continuous, decentralised file synchronisation between devices.
## Access
- Web UI: http://localhost:8384
- Web UI: http://localhost:${WEB_PORT}
- First run: go to **Settings → GUI** and set a username and password.
$( [ "$DISCOVERY_PORT" != "21027" ] && echo "
**Note:** the default ports were already in use, so this instance's ports
were shifted — see below. Local LAN auto-discovery only works at the
standard 21027/udp; with a shifted discovery port, add other devices
manually by device ID instead of relying on auto-discovery." )
## Firewall ports (for LAN sync)
Open these on the host firewall so other Syncthing devices can reach this node:
\`\`\`
sudo ufw allow 22000/tcp comment "Syncthing sync protocol"
sudo ufw allow 22000/udp comment "Syncthing sync protocol (QUIC)"
sudo ufw allow 21027/udp comment "Syncthing local discovery"
sudo ufw allow ${SYNC_PORT}/tcp comment "Syncthing sync protocol"
sudo ufw allow ${SYNC_PORT}/udp comment "Syncthing sync protocol (QUIC)"
sudo ufw allow ${DISCOVERY_PORT}/udp comment "Syncthing local discovery"
\`\`\`
## Manage
@@ -266,7 +301,7 @@ MD
|| log_warning "Start failed — check: docker compose logs"
fi
echo " Access at: http://localhost:8384"
echo " Access at: http://localhost:${WEB_PORT}"
echo " First run: set a username and password in Settings → GUI"
echo ""
}