# In \`community.docker\` collection what is the equivalent way of \`docker ps\` i.e., getting running containers on a device?

**URL:** <https://forum.ansible.com/t/in-community-docker-collection-what-is-the-equivalent-way-of-docker-ps-i-e-getting-running-containers-on-a-device/5926>\
**Category:** Get Help\
**Tags:** ansible, docker\
**Created:** [May 22, 2024, 6:50am UTC](https://forum.ansible.com/t/in-community-docker-collection-what-is-the-equivalent-way-of-docker-ps-i-e-getting-running-containers-on-a-device/5926 "2024-05-22T06:50:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShanDesai](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@ShanDesai](https://forum.ansible.com/u/ShanDesai)\
**Post date:** [May 22, 2024, 6:50am UTC](https://forum.ansible.com/t/in-community-docker-collection-what-is-the-equivalent-way-of-docker-ps-i-e-getting-running-containers-on-a-device/5926/1 "2024-05-22T06:50:49Z")

</div>

I am aware of `community.docker.docker_container_info` where one can mention the name of the container and obtain information about it. However I cannot find any information on what is the equivalent to obtaining a list of currently running containers on the device.

My goal is to perform a complete prune of the device, however the `community.docker.docker_prune` only provides pruning of non-running containers.

---

<div class="post-metadata">

**Author:** ![ShanDesai](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@ShanDesai](https://forum.ansible.com/u/ShanDesai)\
**Post date:** [May 22, 2024, 7:06am UTC](https://forum.ansible.com/t/in-community-docker-collection-what-is-the-equivalent-way-of-docker-ps-i-e-getting-running-containers-on-a-device/5926/2 "2024-05-22T07:06:07Z")

</div>

The only solution I have at the moment is:

```yaml
   - name: '(DEVICE) - Running Containers Information'
      ansible.builtin.command:
        cmd: docker ps -a --format=json
      register: running_containers_info
      changed_when: running_containers_info.rc != 0
    - debug: msg="{{ item | from_json }}"
      loop: "{{ running_containers_info.stdout_lines }}"
    - community.docker.docker_container:
        name: "{{ container_info.Names }}"
        state: absent
      vars:
        container_info: "{{ item | from_json }}"
      loop: "{{ running_containers_info.stdout_lines }}"
    - name: '(DEVICE) - Prune system'
      community.docker.docker_prune:
        containers: true
        images: true
        images_filters:
          dangling: false
        networks: true
        builder_cache: true

```

because `docker ps -a --format=json` provides distinct JSON structures as opposed to a list of JSON, this is why I need to format from `from_json` gymnastics in the middle.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [May 22, 2024, 11:46am UTC](https://forum.ansible.com/t/in-community-docker-collection-what-is-the-equivalent-way-of-docker-ps-i-e-getting-running-containers-on-a-device/5926/3 "2024-05-22T11:46:04Z")

</div>

That looks fine to me, one thing I’d do differently though is this:

```yaml
   - name: '(DEVICE) - Running Containers Information'
      ansible.builtin.command:
        cmd: docker ps -a --format=json
      register: running_containers_info
      changed_when: false # 👈

```

As running this command doesn’t change anything.

I’d probably also set a fact for the `docker ps -a --format=json` output with a [dict2items](https://ansible.readthedocs.io/projects/ansible-core/en/stable-2.14/collections/ansible/builtin/dict2items_filter.html) filter and then loop on that, but the way you have done it is fine, if it works 🙂 .

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [May 22, 2024, 5:36pm UTC](https://forum.ansible.com/t/in-community-docker-collection-what-is-the-equivalent-way-of-docker-ps-i-e-getting-running-containers-on-a-device/5926/4 "2024-05-22T17:36:21Z")

</div>

Or simply use the [community.docker.docker\_host\_info](https://docs.ansible.com/ansible/devel/collections/community/docker/docker_host_info_module.html#community-docker-docker-host-info-module-retrieves-facts-about-docker-host-and-lists-of-objects-of-the-services) module with [`containers=true`](https://docs.ansible.com/ansible/devel/collections/community/docker/docker_host_info_module.html#parameter-containers) and [`containers_all=true`](https://docs.ansible.com/ansible/devel/collections/community/docker/docker_host_info_module.html#parameter-containers_all) instead of using the `command` module.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 21, 2024, 5:36pm UTC](https://forum.ansible.com/t/in-community-docker-collection-what-is-the-equivalent-way-of-docker-ps-i-e-getting-running-containers-on-a-device/5926/5 "2024-06-21T17:36:32Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
