title: docker-compose unlink network from child containers when stopping parent containers?
body: <p>This is a continuation of my journey <a href="https://stackoverflow.com/questions/59657572/docker-compose-multiple-projects">of creating multiple docker projects dynamically</a>. I did not mention previously, to make this process dynamica as I want devs to specify what project they want to use, I'm using ansible to up local env.</p>
  
  <p><strong>Logic is:</strong></p>
  
  <ol>
  <li>running <code>ansible-playbook run.yml -e "{projectsList:
  ['app-admin']}"</code> - providing list of projects I want to start</li>
  <li>stop existing main containers (in case they are running from the previous time)</li>
  <li>Start the main containers</li>
  <li>Depend on the provided list of projects run role tasks () I have a separate role for each supported project</li>
  <li>stop the existing child project containers (in case they are running from the previous time)</li>
  <li>start the child project containers</li>
  <li>make some configuration depend on the role</li>
  </ol>
  
  <p>And here is the issue (again) with the network, when I stop the main containers it's failing with a message:</p>
  
  <blockquote>
    <p>error while removing network: network appnetwork has active endpoints</p>
  </blockquote>
  
  <p>it makes sense as child docker containers use the same network, but I do not see so far way to change ordering of tasks as I'm using the roles, so main docker tasks always running before role-specific tasks.</p>
  
  <p><strong>main ansible file:</strong></p>
  
  <pre><code>---
  #- import_playbook: './services/old.yml'
  - hosts: localhost
    gather_facts: true
    vars:
      # add list of all supported projects, THIS SHOULD BE UPDATED FOREACH NEW PROJECT!
      supportedProjects: ['all', 'app-admin', 'app-landing']
    vars_prompt:
      - name: "ansible_become_pass"
        prompt: "Sudo password"
        private: yes
    pre_tasks:
      # List of projects should be provided
      - fail: msg="List of projects you want to run playbook for not provided"
        when: (projectsList is not  defined) or (projectsList|length == 0)
      # Remove unsupported projects from list
      - name: Filter out not supported projects
        set_fact:
          filteredProjectsList: "{{ projectsList | intersect(supportedProjects) }}"
      # Check if any of projects exist after filtering
      - fail: msg="All project you provided not supported. Supported projects {{ supportedProjects }}"
        when: filteredProjectsList|length == 0
      # Always stop existing docker containers
      - name: stop existing common app docker containers
        docker_compose:
          project_src: ../docker/common/
          state: absent
      - name: start common app  docker containers like nginx proxy, redic, mailcatcher etc. (this can take a while if running by the first time)
        docker_compose:
          project_src: ../docker/common/
          state: present
          build: no
          nocache: no
      - name: Get www-data id
        command: docker exec app-php id -u www-data
        register: wwwid
      - name: Get current user group id
        command: id -g
        register: userid
      - name: Register user and www-data ids
        set_fact:
          userid: "{{userid.stdout}}"
          wwwdataid: "{{wwwid.stdout}}"
    roles:
  
      - { role: app-landing, when: '"app-landing" in filteredProjectsList or "all" in filteredProjectsList' }
      - { role: app-admin,  when:  ("app-admin" in filteredProjectsList) or ("all" in filteredProjectsList) }
  </code></pre>
  
  <p>and role example <code>app-admin/tasks/mian.yml</code>:</p>
  
  <pre><code>---
  - name: Sync {{name}} with git (can take while to clone repo by the first time)
    git:
      repo: "{{gitPath}}"
      dest: "{{destinationPath}}"
      version: "{{branch}}"
  - name: stop existing {{name}} docker containers
    docker_compose:
      project_src: "{{dockerComposeFileDestination}}"
      state: absent
  - name: start {{name}} docker containers (this can take a while if running by the first time)
    docker_compose:
      project_src: "{{dockerComposeFileDestination}}"
      state: present
      build: no
      nocache: no
  - name: Copy {{name}} env file
    copy:
      src: development.env
      dest: "{{destinationPath}}.env"
      force: no
  - name: Set file permissions for local {{name}} project files
    command:  chmod -R ug+w {{projectPath}}
    become: yes
  - name: Set execute permissions for local {{name}} bin folder
    command: chmod -R +x {{projectPath}}/bin
    become: yes
  - name: Set user/group for {{name}} to {{wwwdataid}}:{{userid}}
    command: chown -R {{wwwdataid}}:{{userid}} {{projectPath}}
    become: yes
  - name: Composer install for {{name}}
    command: docker-compose -f {{mainDockerComposeFileDestination}}docker-compose.yml exec -T app-php sh -c "cd {{containerProjectPath}} &amp;&amp; composer install"
  </code></pre>
  
  <p>Maybe there is a way to somehow unlink the network if the main container stop. I thought when a child container network set like external:</p>
  
  <pre><code>networks:
    appnetwork:
      external: true
  </code></pre>
  
  <p>solves the issue, but it's not.</p>