Search code examples
javaspring-bootdockerdatadog

Datadog: Error while getting hostname, exiting: unable to reliably determine the host name


I'm encountering a connection issue when running a Spring application within a Docker container. The application attempts to connect to localhost:8125, even though I've disabled Datadog from app.yml, and the issue persists only when running within Docker.

WARN |2024-01-11T11:09:56,202|trace=,span=|FluxReceive|[ea7d57a7, L:/127.0.0.1:43453 - R:localhost/127.0.0.1:8125] An exception has been observed post termination, use DEBUG level to see the full stack: java.net.PortUnreachableException: recvAddress(..) failed: Connection refused

Step-by-Step Problem Description:

The application runs without issues when executed without Docker.

Dockerfile content:

ARG JAVA_BASE_IMAGE
FROM $JAVA_BASE_IMAGE

USER appuser
WORKDIR /home/appuser

COPY --chown=appuser:appuser target/*-exec.jar /home/appuser/app.jar
COPY --chown=appuser:appuser entrypoint.sh /home/appuser/entrypoint.sh

RUN chmod 0540 /home/appuser/entrypoint.sh

EXPOSE 8080

ENTRYPOINT ["/home/appuser/entrypoint.sh"]

entrypoint:

#!/bin/bash

java $JVM_OPTS -jar $HOME/app.jar

in the app.yml

management:
  endpoints.web:
    exposure.include: '*' # Endpoint IDs that should be included or '*' for all.
    exposure.exclude:  # Endpoint IDs that should be excluded or '*' for all.
    base-path: /admin # Base path for Web endpoints. Relative to server.servlet.context-path or management.server.servlet.context-path if management.server.port is configured.

  # HEALTH ENDPOINT
  endpoint:
    health.cache.time-to-live: 0ms # Maximum time that a response can be cached.
    health:
      enabled: true # Whether to enable the health endpoint.
      roles:  # Roles used to determine whether or not a user is authorized to be shown details. When empty, all authenticated users are authorized.
      show-details: always # When to show full health details.
  statsd:
    metrics:
      export:
        enabled: false
        flavor: datadog
        host: ${METRICS_DATADOG_STATSDHOST:localhost}
        port: ${METRICS_DATADOG_STATSDPORT:8125}

Solution

  • Considering datadogs lived in an external container to reach it from the same host but external container the right config was

    metrics:
      export:
        enabled: false
        flavor: datadog
        host: ${METRICS_DATADOG_STATSDHOST:host.docker.internal}
        port: ${METRICS_DATADOG_STATSDPORT:8125}