docker-archive / toolbox

The Docker Toolbox
https://docker.com/toolbox
3.22k stars 1.23k forks source link

host.docker.internal doesn't work for Prometheus #823

Open dejangeci opened 5 years ago

dejangeci commented 5 years ago

Issue: Prometheus container cannot scrape Docker host on which it's hosted.

  - job_name: docker-workers
    static_configs:
      - targets: [host.docker.internal:9323]

Prometheus targets shows error:

Get http://host.docker.internal:9323/metrics: dial tcp: lookup host.docker.internal on 127.0.0.11:53: no such host

Env: Windows 7 x64 Docker Toolbox on VirtualBox

Machine created with:

docker-machine create \
  --driver virtualbox \
  --engine-opt experimental \
  --engine-opt metrics-addr=0.0.0.0:9323 \
  default

On Docker Desktop (Windows 10, HyperV) it works without issues.

pun-ky commented 5 years ago

also noticed (in separate project) that "host.docker.internal" does not work on Toolbox but on Desktop yep.

does sb have this issue resolved? / have it working on Toolbox?

Tymek commented 5 years ago

I'm having exact same issue on Debian 9, outside of toolbox framework

pun-ky commented 5 years ago

I solved this issue by putting

extra_hosts:
   host.docker.internal:10.0.2.2
Ajinkya-Suranis commented 5 years ago

I solved this issue by putting

extra_hosts:
   host.docker.internal:10.0.2.2

Thanks. Worked for me! How did you come up with this IP?

borisgr04 commented 4 years ago

Resolví este problema poniendo

extra_hosts:
   host.docker.internal:10.0.2.2

Gracias. ¡Trabajó para mi! ¿Cómo se te ocurrió esta IP?

Where???

pun-ky commented 4 years ago

As I remember correctly, checked routing table after starting some dummy container then checked IPs one after each other empirically. That IP is mostly time constant, however intermittently I got corner case when that IP was different. As I am author of Gradle Environment Plugin helping working with Docker/Swarm I introduced heuristic method for resolving this IP:

https://github.com/Cognifide/gradle-environment-plugin/blob/master/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Toolbox.kt#L15 https://github.com/Cognifide/gradle-environment-plugin/blob/master/src/main/kotlin/com/cognifide/gradle/environment/docker/runtime/Base.kt#L13 which could be a good explanation how I resolved it ;)

docker run alpine /bin/ash -c ip -4 route list match 0/0 | cut -d ' ' -f 3

qiangli commented 4 years ago

workaround:

./entrypoint.sh

#!/bin/sh
set -e
#
function dig() {
    nslookup host.docker.internal|grep Address:|grep -v 127.0.0|cut -d: -f2
}
HOST_IP=$(dig; while [ $? -ne 0 ]; do dig; done)

echo "$HOST_IP host.docker.internal" >> /etc/hosts

exec /bin/prometheus "$@"

compose.yml


    user: root
    entrypoint: /entrypoint.sh
    volumes:
      - ./entrypoint.sh:/entrypoint.sh