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:
+26
-4
@@ -48,6 +48,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
|
||||
@@ -198,6 +213,7 @@ install_arm() {
|
||||
|
||||
local ARM_DIR="$DOCKER_DIR/arm"
|
||||
local DEFAULT_OUTPUT="$ACTUAL_HOME/ripped"
|
||||
local WEB_PORT="8080"
|
||||
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
echo "[DRY-RUN] A.R.M. would:"
|
||||
@@ -205,7 +221,7 @@ install_arm() {
|
||||
echo " - Detect optical drives (/dev/sr*) — defaults to /dev/sr0"
|
||||
echo " - Create ripped output dirs (movies/ music/) under $DEFAULT_OUTPUT"
|
||||
echo " - Run as UID/GID $(id -u "$ACTUAL_USER")/$(id -g "$ACTUAL_USER") with privileged: true"
|
||||
echo " - Expose port 8080"
|
||||
echo " - Expose port 8080 (auto-scanned for a free host port)"
|
||||
echo " - Offer a Caddy reverse proxy and to start the container"
|
||||
return 0
|
||||
fi
|
||||
@@ -225,6 +241,12 @@ install_arm() {
|
||||
OPTICAL_DRIVES="/dev/sr0"
|
||||
fi
|
||||
|
||||
# Scan for a free host port — this default (8080) isn't unique to A.R.M.
|
||||
# in this repo (nextcloud also defaults to 8080), so a plain install
|
||||
# shouldn't silently claim a port another already-running service holds.
|
||||
# See CLAUDE.md's "Port collision avoidance" section.
|
||||
find_free_port WEB_PORT "$WEB_PORT"
|
||||
|
||||
mkdir -p "$ARM_DIR"
|
||||
ensure_docker_dir_ownership "$ARM_DIR"
|
||||
cd "$ARM_DIR" || return 1
|
||||
@@ -275,7 +297,7 @@ services:
|
||||
- \${ARM_OUTPUT}/movies:/home/arm/media/completed
|
||||
- \${ARM_OUTPUT}/music:/home/arm/music
|
||||
ports:
|
||||
- "8080:8080"
|
||||
- "${WEB_PORT}:8080"
|
||||
devices:
|
||||
- /dev/sr0:/dev/sr0
|
||||
# Add more optical drives as needed:
|
||||
@@ -303,7 +325,7 @@ ARM_ENV
|
||||
Auto-rips DVDs, Blu-rays, and CDs when you insert them — identifies the disc,
|
||||
fetches metadata, and transcodes to a usable format.
|
||||
|
||||
- Web UI: http://localhost:8080 (complete setup on first visit)
|
||||
- Web UI: http://localhost:${WEB_PORT} (complete setup on first visit)
|
||||
- Ripped output: \`$ARM_OUTPUT\` → movies and music subdirs
|
||||
- App data: \`config/\` and \`logs/\`
|
||||
|
||||
@@ -338,7 +360,7 @@ MD
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo " Access at: http://localhost:8080"
|
||||
echo " Access at: http://localhost:${WEB_PORT}"
|
||||
echo " Complete setup in browser on first visit."
|
||||
echo ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user