keycloak in docker compose with healthcheck

Docker images from quay.io/keycloak/keycloak don’t contain any commands for a usual healthcheck and installing software is also not trivial. In a discussion on https://github.com/keycloak/keycloak/issues/17273 I found a solution that checks /proc/net/tcp for an open port 8080. That file contains all open ports in hex format. cat and grep are included in the image. So a healthcheck can be realised without installing any additional software.

As running the container isn’t that trivial and documentation is a bit hard to find – here my full docker compose config with the healthcheck:

  keycloak:
    image: quay.io/keycloak/keycloak:latest
    hostname: keycloak
    container_name: keycloak
    command:
      - start-dev
    environment:
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: s3cr3t
    ports:
      - "127.0.0.1:8080:8080"
    healthcheck:
      test: cat /proc/net/tcp | grep '00000000:1F90 00000000:0000' || exit 1
      interval: 5s
      timeout: 2s
      retries: 20
      start_period: 10s