From 10ef7fb68c895355291c63ed6b5004db34fc0ee8 Mon Sep 17 00:00:00 2001 From: Ryan Westfall Date: Thu, 6 Aug 2026 10:45:54 -0500 Subject: [PATCH] Free web-static ports by removing containers that publish them Before compose up, docker rm -f any container bound to the host ports from host_apps so orphan/legacy publishers cannot block the nginx bind; force-recreate when cleanup ran. --- IMPLEMENTATION.md | 4 +++- roles/web-static/tasks/main.yml | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/IMPLEMENTATION.md b/IMPLEMENTATION.md index c967d6f..4868b54 100644 --- a/IMPLEMENTATION.md +++ b/IMPLEMENTATION.md @@ -259,7 +259,9 @@ chat_backend secrets must use `SEARXNG_BASE_URL=http://10.0.0.128:8088`. (writes to the app's `webroot_pattern`, e.g. `/var/www/{env}.realpath.app/html` or `/var/www/{env}.chat.aimloperations/html`). - **web-static** role: one nginx container per app host serving the static roots - on their ports (from `host_apps`); NPM balances across hosts. + on their ports (from `host_apps`); NPM balances across hosts. Before `compose up`, + removes any container currently publishing those host ports (`docker ps --filter + publish=`) so leftovers cannot block the bind, then recreates web-static. ### Reverse proxy / load balancing (NPM at 10.0.0.230) diff --git a/roles/web-static/tasks/main.yml b/roles/web-static/tasks/main.yml index a30ee35..3e42432 100644 --- a/roles/web-static/tasks/main.yml +++ b/roles/web-static/tasks/main.yml @@ -39,9 +39,34 @@ mode: "0644" register: _compose_file + # Free host ports before bind. Catches leftover/orphan containers from older + # compose projects that still publish 8080/8081/etc. Includes the current + # web-static container when present — compose up recreates it next. + - name: web-static | find containers publishing static ports + ansible.builtin.command: + cmd: docker ps -q --filter publish={{ item }} + loop: "{{ _static_apps | map(attribute='port') | list | unique }}" + register: _port_holders + changed_when: false + become: true + + - name: web-static | unique container ids on static ports + ansible.builtin.set_fact: + _stale_port_cids: "{{ _port_holders.results | map(attribute='stdout_lines') | flatten | unique | list }}" + + - name: web-static | remove containers holding static ports + ansible.builtin.command: + cmd: "docker rm -f {{ _stale_port_cids | join(' ') }}" + when: _stale_port_cids | length > 0 + become: true + changed_when: true + register: _stale_port_rm + - name: web-static | start container ansible.builtin.command: - cmd: "docker compose up -d --remove-orphans{{ ' --force-recreate' if (_compose_file is changed) else '' }}" + cmd: >- + docker compose up -d --remove-orphans + {{ '--force-recreate' if (_compose_file is changed or (_stale_port_rm is changed)) else '' }} chdir: "{{ web_static_dir }}" become: true become_user: "{{ admin_user }}" @@ -53,5 +78,8 @@ chdir: "{{ web_static_dir }}" become: true become_user: "{{ admin_user }}" - when: _nginx_conf is changed and _compose_file is not changed + when: + - _nginx_conf is changed + - _compose_file is not changed + - _stale_port_rm is not changed changed_when: true