dotnet / aspire

Tools, templates, and packages to accelerate building observable, production-ready apps
https://learn.microsoft.com/dotnet/aspire
MIT License
3.94k stars 481 forks source link

Include a healthcheck binary for use in a docker compose environment #3407

Open rgl opened 7 months ago

rgl commented 7 months ago

in order to have a healthcheck in docker compose we need a command that returns a 0 exit code.

we either need a dashboard sub-command (e.g. dotnet /app/Aspire.Dashboard.dll readyz) or an http client (e.g. curl) binary available inside the container.

a healthcheck endpoint should also be available, so we can use this in other places, like k8s.

for example, in this hypothetical docker compose file, I'm using curl (but a better approach would be to implement something equivalent of this example, but as a dashboard sub-command, e.g., dotnet /app/Aspire.Dashboard.dll readyz):

# see https://github.com/compose-spec/compose-spec/blob/master/spec.md
# see https://github.com/opencontainers/image-spec/blob/master/annotations.md
services:
  aspire-dashboard:
    # see https://mcr.microsoft.com/product/dotnet/nightly/aspire-dashboard/about
    # see https://github.com/dotnet/dotnet-docker/issues/5128
    # see https://github.com/dotnet/aspire/issues/2248#issuecomment-1947902486
    # see https://github.com/dotnet/aspire/tree/main/src/Aspire.Dashboard
    image: mcr.microsoft.com/dotnet/nightly/aspire-dashboard:8.0.0-preview.5
    environment:
      - DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS=true
    ports:
      # web ui.
      - 18888:18888
      # otlp grpc.
      #- 18889:18889
    healthcheck:
      test: ["CMD", "curl", "--silent", "--fail-with-body", "--max-time", "5", "http://localhost:18888/healthz/ready"]
      interval: 15s
      timeout: 5s
      retries: 2
    restart: on-failure
davidfowl commented 7 months ago

Why does it need to be a binary? Why not an endpoint?

rgl commented 7 months ago

docker (and docker compose) only support healthchecks by calling a binary (e.g. as the healthcheck.test command displayed above). Have a look at the Dockerfile HEALTHCHECK instruction documentation.

jeff-techstension commented 6 months ago

What's wrong with the example where they append || exit 1

HEALTHCHECK --interval=5m --timeout=3s \
  [CMD](https://docs.docker.com/reference/dockerfile/#cmd) curl -f http://localhost/ || exit 1