@@ -148,6 +148,7 @@ service_active() { systemctl is-active --quiet "$1"; }
148148docker_safe_rm () { docker rm -f " $1 " > /dev/null 2>&1 || true ; }
149149docker_exists () { docker ps -a --format ' {{.Names}}' | grep -Fxq " $1 " ; }
150150docker_running () { docker ps --format ' {{.Names}}' | grep -Fxq " $1 " ; }
151+ port_in_use () { ss -ltn " ( sport = :$1 )" | awk ' NR>1 {print}' | grep -q . ; }
151152
152153http_ok () {
153154 local url=" $1 "
@@ -484,6 +485,36 @@ stop_active_container() {
484485 fi
485486}
486487
488+ ensure_host_port_available () {
489+ step " Ensuring host port ${HOST_PORT} is available"
490+
491+ if ! port_in_use " ${HOST_PORT} " ; then
492+ ok " Host port ${HOST_PORT} is available"
493+ return 0
494+ fi
495+
496+ warn " Host port ${HOST_PORT} is currently in use; attempting to free common proxy services"
497+ for svc in nginx apache2 caddy haproxy traefik; do
498+ if service_active " ${svc} " ; then
499+ warn " Stopping service using host networking: ${svc} "
500+ systemctl stop " ${svc} " || true
501+ systemctl disable " ${svc} " > /dev/null 2>&1 || true
502+ fi
503+ done
504+
505+ if ! port_in_use " ${HOST_PORT} " ; then
506+ ok " Host port ${HOST_PORT} is now free"
507+ return 0
508+ fi
509+
510+ err " Host port ${HOST_PORT} is still in use"
511+ warn " Active listeners:"
512+ ss -ltnp " ( sport = :${HOST_PORT} )" || true
513+ warn " Containers publishing host port ${HOST_PORT} :"
514+ docker ps --filter " publish=${HOST_PORT} " --format ' - {{.Names}} ({{.Ports}})' || true
515+ return 1
516+ }
517+
487518start_target_container () {
488519 step " Launching target ${TARGET_COLOR} container: ${TARGET_NAME} on host port ${TARGET_PORT} "
489520 docker_safe_rm " ${TARGET_NAME} "
@@ -600,6 +631,7 @@ ensure_ebs_volume
600631
601632 # 1) Stop active (frees CPU/RAM/port)
602633stop_active_container
634+ ensure_host_port_available
603635prepare_langflow_env
604636ensure_postgres
605637
@@ -614,4 +646,4 @@ verify_domain
614646# 4) Success → optionally prune old container
615647ok " Deployment successful: ${TARGET_COLOR} is live"
616648cleanup_old_container
617- DEPLOY_SUCCESS=1
649+ DEPLOY_SUCCESS=1
0 commit comments