docker-停止父容器时从子容器中取消网络链接?

docker-compose unlink network from child containers when stopping parent containers?

这是我旅程的延续。我之前没有提到,为了使这个过程动态化,因为我希望开发人员指定他们想要使用的项目,我正在使用 ansible 来提升本地环境。

逻辑是:

  1. 运行ning ansible-playbook run.yml -e "{projectsList: ['app-admin']}" - 提供我想开始的项目列表
  2. 停止现有的主容器(以防它们是 运行上次的容器)
  3. 启动主容器
  4. 取决于提供的项目列表运行 role tasks () 我对每个支持的项目都有一个单独的角色
  5. 停止现有的子项目容器(以防它们 运行ning 从以前开始)
  6. 启动子项目容器
  7. 根据角色进行一些配置

这是(再次)网络问题,当我停止主容器时它失败并显示一条消息:

error while removing network: network appnetwork has active endpoints

这很有意义,因为子 docker 容器使用相同的网络,但到目前为止我还没有看到更改任务顺序的方法,因为我正在使用角色,所以主要 docker 任务总是 运行在角色特定的任务之前。

主要ansible文件:

---
#- 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) }

和角色示例app-admin/tasks/mian.yml:

---
- 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}} && composer install"

也许有一种方法可以在主容器停止时以某种方式断开网络链接。我想当子容器网络设置为外部时:

networks:
  appnetwork:
    external: true

问题解决了,但没有。

使用外部网络的快速实验:

dc1/dc1.yml

version: "3.0"

services:
  nginx:
    image: nginx
    ports: 
      - "8080:80"
    networks: 
      - an0

networks:
  an0:
    external: true

dc2/dc2.yml

version: "3.0"

services:
  redis:
    image: redis
    ports: 
      - "6379:6379"
    networks: 
      - an0

networks:
  an0:
    external: true

开始和停止:

$ docker network create -d bridge an0
1e07251e32b0d3248b6e70aa70a0e0d0a94e457741ef553ca5f100f5cec4dea3

$ docker-compose -f dc1/dc1.yml up -d
Creating dc1_nginx_1 ... done

$ docker-compose -f dc2/dc2.yml up -d
Creating dc2_redis_1 ... done

$ docker-compose -f dc1/dc1.yml down
Stopping dc1_nginx_1 ... done
Removing dc1_nginx_1 ... done
Network an0 is external, skipping

$ docker-compose -f dc2/dc2.yml down
Stopping dc2_redis_1 ... done
Removing dc2_redis_1 ... done
Network an0 is external, skipping