fix: extract and create external Docker networks from compose files
Some checks failed
🚀 Build & Deploy Image / Determine Build Necessity (push) Successful in 28s
Security Vulnerability Scan / Check for Dependency Changes (push) Successful in 28s
🚀 Build & Deploy Image / Build Runtime Base Image (push) Successful in 11s
🚀 Build & Deploy Image / Run Tests & Quality Checks (push) Has been skipped
Security Vulnerability Scan / Composer Security Audit (push) Has been skipped
🚀 Build & Deploy Image / Build Docker Image (push) Successful in 12s
🚀 Build & Deploy Image / Auto-deploy to Staging (push) Failing after 50s
🚀 Build & Deploy Image / Auto-deploy to Production (push) Has been skipped

- Extract external networks from docker-compose.base.yml and compose override files
- Extract network names from 'name:' fields when external: true
- Create all required external networks before docker compose up
- Fixes error: 'network postgres-staging-internal declared as external, but could not be found'

This ensures all external networks (traefik-public, app-internal, postgres-staging-internal, etc.)
are created before attempting to start containers.
This commit is contained in:
2025-11-09 01:45:35 +01:00
parent 78f6fb186b
commit 453e573f28

View File

@@ -259,13 +259,47 @@
failed_when: false failed_when: false
changed_when: compose_update_alt.changed | default(false) changed_when: compose_update_alt.changed | default(false)
- name: Extract external networks from docker-compose files
ansible.builtin.shell: |
cd {{ application_code_dest }}
grep -h "external:" docker-compose.base.yml docker-compose.{{ application_compose_suffix }} 2>/dev/null | \
grep -B 5 "external: true" | \
grep -E "^\s+[a-zA-Z0-9_-]+:" | \
sed 's/://' | \
sed 's/^[[:space:]]*//' | \
sort -u || echo ""
register: external_networks_raw
changed_when: false
failed_when: false
- name: Extract external network names (with name: field)
ansible.builtin.shell: |
cd {{ application_code_dest }}
grep -A 2 "external: true" docker-compose.base.yml docker-compose.{{ application_compose_suffix }} 2>/dev/null | \
grep "name:" | \
sed 's/.*name:[[:space:]]*//' | \
sed 's/"//g' | \
sort -u || echo ""
register: external_network_names
changed_when: false
failed_when: false
- name: Set list of external networks to create
ansible.builtin.set_fact:
external_networks_to_create: >-
{%- set networks_from_keys = external_networks_raw.stdout | trim | split('\n') | select('match', '.+') | list -%}
{%- set networks_from_names = external_network_names.stdout | trim | split('\n') | select('match', '.+') | list -%}
{%- set all_networks = (networks_from_keys + networks_from_names) | unique | list -%}
{%- set default_networks = ['traefik-public', 'app-internal'] -%}
{%- set final_networks = (all_networks + default_networks) | unique | list -%}
{{ final_networks }}
- name: Ensure Docker networks exist - name: Ensure Docker networks exist
community.docker.docker_network: community.docker.docker_network:
name: "{{ item }}" name: "{{ item }}"
state: present state: present
loop: driver: bridge
- traefik-public loop: "{{ external_networks_to_create | default(['traefik-public', 'app-internal']) }}"
- app-internal
ignore_errors: yes ignore_errors: yes
- name: Check if .env file exists - name: Check if .env file exists