Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List Stopped Containers using docker_host_info #535

Closed
RedlineTriad opened this issue Dec 17, 2022 · 7 comments · Fixed by #538
Closed

List Stopped Containers using docker_host_info #535

RedlineTriad opened this issue Dec 17, 2022 · 7 comments · Fixed by #538
Labels
docker-plain plain Docker (no swarm, no compose, no stack) question Further information is requested

Comments

@RedlineTriad
Copy link

SUMMARY

There is no way I found to get the list of stopped containers.
This should be possible with some kind of module option.

ISSUE TYPE
  • Feature Idea
COMPONENT NAME

docker_host_info

ADDITIONAL INFORMATION

For my molecule test suite, I want to make sure that no containers crashed, or stopped for any other reasons.
However, when I stopped containers, verification still passed, because stopped containers were not included in the results.

To achieve this I used the following code:

---
- name: Verify
  hosts: all
  gather_facts: false
  tasks:
    - name: Get Docker Version
      community.docker.docker_host_info:
        verbose_output: yes
        containers: yes
      register: docker_host_info
      become: true

    - name: Debug
      ansible.builtin.debug:
        var: docker_host_info.containers

    - name: Get List of non-running Containers
      ansible.builtin.set_fact:
        non_running_containers: >-
          {{
            docker_host_info.containers 
            | rejectattr('State', 'eq', 'running')
          }}

    - name: Ensure No Applications are Stopped
      ansible.builtin.assert:
        that: not non_running_containers 
        fail_msg: >-
          Expected all containers to be running, found:
          {{ non_running_containers }}
@felixfontein
Copy link
Collaborator

Can't you use containers_filters for this, with filter status: exited?

@felixfontein felixfontein added question Further information is requested docker-plain plain Docker (no swarm, no compose, no stack) labels Dec 17, 2022
@RedlineTriad
Copy link
Author

That does seem to work, but:

  1. It would be nice if there was maybe an example in the docs.
  2. I would also like to get all containers regardless of state for other filtering.
  3. There exist a lot of other states, and it would be annoying to query them individually.
    According to this random blog from google all these states exist: https://www.baeldung.com/ops/docker-container-states
    • created
    • running
    • restarting
    • exited
    • paused
    • dead

But thanks for the info @felixfontein, might help me work around it better.

@felixfontein
Copy link
Collaborator

It would be nice if there was maybe an example in the docs.

This is a community project, feel free to create a PR to add such an example :)

I would also like to get all containers regardless of state for other filtering.

An "all" option (like in docker container list --all) would definitely be a good idea.

According to this random blog from google all these states exist: https://www.baeldung.com/ops/docker-container-states

You can also look here for a more authoritative source: https://github.com/moby/moby/blob/13b36ce06352ff9853ee5dc2ef1d5b33b85e17f4/container/state.go#L147-L158

@felixfontein
Copy link
Collaborator

I've implemented an containers_all option in #538.

@RedlineTriad
Copy link
Author

Oh, thank you a lot @felixfontein !
I was thinking of maybe trying to do it myself, but am busy working on my private server.
Setting up SSO for a single user is reasonable use of time right? :D

@felixfontein
Copy link
Collaborator

Setting up SSO for a single user is reasonable use of time right? :D

It certainly is, if that user is very important to you! :D

@RedlineTriad
Copy link
Author

That user is me :)
I guess self care does count as important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docker-plain plain Docker (no swarm, no compose, no stack) question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants