Include a healthcheck binary for use in a docker compose environment
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
Why does it need to be a binary? Why not an endpoint?
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.
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
What's wrong with the example where they append
|| exit 1HEALTHCHECK --interval=5m --timeout=3s \ [CMD](https://docs.docker.com/reference/dockerfile/#cmd) curl -f http://localhost/ || exit 1
curl or any similar command isn't included in the aspire docker image.
It also looks like the image doesn't include any shells that could help.