Ping endpoint working but docker healthcheck fails

My docker-compose.yml contains this:

services:
  traefik:
    image: "traefik:v2.4"
    container_name: "traefik"
    deploy:
      labels:
        - traefik.enable=true
        - traefik.docker.network=traefik-public
        - traefik.constraint-label=traefik-public
        # ping
        - traefik.http.routers.ping.rule=Host(`traefik.domain.com`) && Path(`/ping`)
        - traefik.http.routers.ping.service=ping@internal
        - traefik.http.routers.ping.tls.certresolver=le
        - traefik.http.routers.ping.tls=true
        [...]
    command:
      [...]
      - --ping=true
      - --ping.entrypoint=websecure
      - --ping.manualRouting=true

This successfully exposes the ping endpoint under https://traefik.domain.com/ping

Now if I want to implement automatic health checks via docker, the container always fail to deploy.
I tried it by adding the healthcheck directive to the docker-compose file:

healthcheck:
  test: ["CMD", "traefik", "healthcheck", "--ping"]
  interval: 30s
  timeout: 3s
  retries: 30

Any ideas? Do I need to somehow tell the healthcheck command the path / subdomain where to reach ping endpoint?

hey,
please have a look on the recommended health check command. You can update the domain that points to Traefik instance and tune other parameters (internal, start_period) according to your needs.

 healthcheck:
      test: wget --quiet --tries=1 --spider http://ping.127.0.0.1.nip.io/ping || exit 1
      interval: 10s
      timeout: 1s
      retries: 3
      start_period: 10s

More details in that repo:

2 Likes

Thank you very much this works! Although I think this should be mentioned in the documentation as it suggests to use the CLI healthcheck command for healthchecks. Ping - Traefik

Glad to hear that it solved your issue. Thanks a lot for your feedback, we will update the documentation accordingly by adding examples of how to implement healthcheck correctly on K8S and Swarm :slight_smile:
:wave:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.