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

Enhance healthcheck blocking dependent behavior like lifecyle #10580

Closed
loynoir opened this issue May 18, 2023 · 6 comments
Closed

Enhance healthcheck blocking dependent behavior like lifecyle #10580

loynoir opened this issue May 18, 2023 · 6 comments

Comments

@loynoir
Copy link

loynoir commented May 18, 2023

Description

Problem

I found postgres image can't block dependent startup, which make dependent start up too early.

Actual

If dependent accept client at early time, dependent failed.

Expected

After postgres ready, dependent should not startup and accept client.

Try

I found .depends_on.X.condition works, but not very graceful to solve wait-for-container-x-before-starting-y.

  • If .interval big, run .healthcheck.test late, startdependent late, not very ideal.

  • If .interval small, run .healthcheck.test too many times, not very graceful.

Workaround

Below is my workaround.

  foo:
    ...
    depends_on:
      bar:
        condition: service_healthy
    ...
 
  bar:
    ...
    tmpfs:
      - /tmp
    healthcheck:
      test: 'if [ -e /tmp/first_run ]; then sleep 24h; else touch /tmp/first_run; bash /script/wait.sh; fi'
      timeout: 25h
      interval: 1s

Feat

Above workaround I still don't think very ideal, so I opened a feat.

Enhance healthcheck behavior like lifecyle.

  foo:
    ...
    depends_on:
      bar:
        condition: service_lifecycle_bootstrap
    ...
 
  bar:
    ...
    lifecycle:
        bootstrap: 'bash /script/wait.sh'

Related

Additional

Are there lifecyle support within container ecosystem?

Yes, vscode devcontainer support lifecycle.

https://github.com/devcontainers/spec/blob/main/schemas/devContainerFeature.schema.json#L106-L206

  • initializeCommand
  • onCreateCommand
  • updateContentCommand
  • postCreateCommand
  • postStartCommand
  • postAttachCommand
@loynoir loynoir changed the title Enhance healthcheck behavior like lifecyle Enhance healthcheck blocking dependent behavior like lifecyle May 18, 2023
@ndeloof
Copy link
Contributor

ndeloof commented May 22, 2023

the devcontainer feature you're listing here is about command hooks to run during container lifecycle, not about dependency management.

IIUC your needs, you have to wait for postgres to be running and accept connexion before your client code get started, otherwise connexion fails. This is exactly what condition: service_healthy is designed for: client container won't get started before their dependency is reported as "healthy" by the container runtime, which is easy to setup as postgres ilmage includes utility tool pg_isready.

healthcheck interval and health status unfortunately is managed by container runtime, not docker compose, so we can't tweak the interval to avoid unnecessary runs once service is started
AFAICT you're looking for readyness check which isn't supported by docker engine (see moby/moby#30860) - maybe we should introduce this feature directly in Docker Compose

@offby1
Copy link

offby1 commented Jul 26, 2024

I've worked around this like so

  postgres: &postgres
    image: postgres:16
    healthcheck:
      test:
        - "CMD"
        - "bash"
        - "-c"
        - "echo 'select 1' | psql --dbname='postgres' --username=postgres"

      # Runs the healthcheck every 12 hours.  We don't particularly want this, but we are forced to specify *some*
      # interval.
      interval: 12h
      timeout: 10s
      retries: 3

      # This is what we really care about -- it amends the "interval" above, by running the health check every second,
      # stopping after 10 seconds, or if the test succeeds, whichever comes first.  The docker docs are not clear about
      # this.
      start_period: 10s
      start_interval: 1s

@jhrotko
Copy link
Contributor

jhrotko commented Oct 14, 2024

@offby1 this might interest you #12166

@offby1
Copy link

offby1 commented Oct 14, 2024

@offby1 this might interest you #12166

Thanks. I read what docs I could find but it's not obvious that this new feature solves the problem in this issue. I assume it does, otherwise you wouldn't have drawn my attention to it. But might the docs for this new feature give an example of it solving this problem? Or at least mention that it can solve this class of problem?

@ndeloof
Copy link
Contributor

ndeloof commented Oct 23, 2024

What you are looking for is start_interval which let you define a higher rate to wait for service to become healthy after it just has been created, then adopt a lower pace once it's up to just offer health status.

@ndeloof ndeloof closed this as completed Oct 23, 2024
@offby1
Copy link

offby1 commented Oct 23, 2024

Yes, my workaround above uses that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants