Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用docker\u host\u信息过滤处于退出状态的docker容器?_Docker_Ansible_Docker Machine_Ansible 2.x_Dockerpy - Fatal编程技术网

如何使用docker\u host\u信息过滤处于退出状态的docker容器?

如何使用docker\u host\u信息过滤处于退出状态的docker容器?,docker,ansible,docker-machine,ansible-2.x,dockerpy,Docker,Ansible,Docker Machine,Ansible 2.x,Dockerpy,是否有其他ansible docker模块来捕获VM上的所有容器,即使是处于退出状态的容器 - name: Get container info docker_host_info: containers: yes register: result docker_host_info仅捕获处于UP状态的容器。是否有方法捕获或过滤处于退出状态的。请提供帮助。您需要使用带有过滤器的选项status=exited 查看此ansible剧本: --- - name: Firs

是否有其他ansible docker模块来捕获VM上的所有容器,即使是处于退出状态的容器

 - name: Get container info
    docker_host_info:
      containers: yes
    register: result
docker_host_info仅捕获处于UP状态的容器。是否有方法捕获或过滤处于退出状态的。请提供帮助。

您需要使用带有过滤器的选项
status=exited

查看此ansible剧本:

---
- name: First playbook
  hosts: 127.0.0.1
  gather_facts: true
  become: true
  tasks:
  - name: Get container info
    docker_host_info:
      containers: yes
      containers_filters:
        status: "exited"
    register: result
  - debug:
      msg: "Exited container list : {{ item['Names'] }}"
    loop: "{{ result.containers }}"

[centos@jumphost tmp]$ sudo ansible-playbook -c local -i 127.0.0.1 a.yaml 
[WARNING]: Unable to parse /var/tmp/127.0.0.1 as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [First playbook] **************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [Get container info] **********************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [debug] ***********************************************************************************************************************************************************************
ok: [127.0.0.1] => (item={u'Status': u'Exited (137) 10 minutes ago', u'Command': u'httpd-foreground', u'Names': [u'/goofy_noyce'], u'Created': 1596717162, u'Image': u'httpd', u'Ports': [], u'Id': u'b2dacc3e5d048f6ae4143e0b76931f104d1dc132e81d378b2456aa5839e459a6'}) => {
    "msg": "Exited container list : [u'/goofy_noyce']"
}
ok: [127.0.0.1] => (item={u'Status': u'Exited (0) About an hour ago', u'Command': u'/bin/sh', u'Names': [u'/dazzling_noyce'], u'Created': 1596714877, u'Image': u'alpine', u'Ports': [], u'Id': u'f54e2aa1c510ece29d8665c65ae5de29552e7e280e7591c399a57a976f80fa81'}) => {
    "msg": "Exited container list : [u'/dazzling_noyce']"
}

PLAY RECAP *************************************************************************************************************************************************************************
127.0.0.1                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[centos@jumphost tmp]$
我的虚拟机上几乎没有退出的容器

[centos@jumphost tmp]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
b2dacc3e5d04        httpd               "httpd-foreground"       6 minutes ago       Exited (137) 6 minutes ago                       goofy_noyce
f54e2aa1c510        alpine              "/bin/sh"                44 minutes ago      Exited (0) 44 minutes ago                        dazzling_noyce
db7789d302db        <no image>          "/bin/sh -c 'touch /…"   2 days ago          Created                                          fervent_morse
3e948852f5cb        <no image>          "/bin/sh -c 'touch /…"   2 days ago          Created                                          goofy_wiles
[centos@jumphost tmp]$
如您所见,只有已退出的容器被列出

更新:

要同时列出正在运行和退出的容器,请使用多个ansible任务

Task 1:
- name: Get exited container info
    |
containers_filters:
        status: "exited"
    |
Task 2
- name: Get running container info
    |
containers_filters:
        status: "running"


如何获取所有docker容器…一个正在运行且处于退出状态的容器。我试过状态:所有的都给我错误。你能帮忙吗<代码>容器\u过滤器:状态:“全部”@SaraJames我无法一次列出正在运行和已退出的容器。你需要多个任务来完成它。检查我的最新答案。你太棒了!!非常感谢您的回答。@SaraJames太好了!您能否将答案标记为已接受,以便其他人知道答案已成功回答。