community.docker icon indicating copy to clipboard operation
community.docker copied to clipboard

state: healthy for docker_container

Open LeanderFS opened this issue 1 year ago • 1 comments

SUMMARY

Currently it's only possible to use state absent, present, stopped and started for the docker_container module. I would like to propose adding state: healthy to have Ansible wait until the integrated HEALTHCHECK returns healthy (when defined in the Dockerfile).

ISSUE TYPE
  • Feature Idea
COMPONENT NAME

community.docker.docker_container

ADDITIONAL INFORMATION

By implementing this feature, a playbook can halt execution until the docker_container healtcheck returns healthy. This prevents additional tasks to poll for the health manually.

- name: Create database container
  docker_container:
    name: "database"
    image: "mysql:latest"
    state: "healthy" # This will trigger Ansible to wait for the HEALTHCHECK statement to return healthy, and fail if unhealthy.
    restart: true
    detach: true
    pull: "always"
    restart_policy: always
    published_ports:
      - "{{ db_fixture_port }}:3306"

LeanderFS avatar Jun 18 '24 13:06 LeanderFS

Another idea would be to (also) have a separate module which allows to wait for several contains at once to reach the healthy state. That would allow users to start several containers first with the docker_container module, and then wait until all of them are healthy afterwards. This is likely faster than waiting for each to be healthy before starting the next.

Your case can then also be split up into just two tasks (without any explicit polling).

Obviously that doesn't mean that a state=healthy (or something similar) doesn't make sense / shouldn't be implemented, I think it's just more flexible to also have a separate module for just waiting :)

felixfontein avatar Jun 18 '24 21:06 felixfontein

I've implemented state=healthy in #921.

felixfontein avatar Jul 07 '24 21:07 felixfontein