无法从Docker容器访问主机

时间:2019-02-27 01:16:26

标签: docker prometheus

使用Docker版本18.09.2。在Windows 10上使用docker。

设置一个prometheus和grafana堆栈以监视在我的本地主机上运行的服务的指标。这是我的docker compose文件。

version: '3.4'

networks:
  monitor-net:
    driver: bridge
  dockernet:
    external: true

volumes:
    prometheus_data: {}
    grafana_data: {}

services:

  prometheus:
    image: prom/prometheus:v2.7.1
    container_name: prometheus
    volumes:
      - ./prometheus/:/etc/prometheus/
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'
    restart: unless-stopped
    expose:
      - 9090
    networks:
      - monitor-net
      - dockernet
    extra_hosts:
      - "localhost1:10.0.75.1"
    labels:
      org.label-schema.group: "monitoring"


  grafana:
    image: grafana/grafana:5.4.3
    container_name: grafana
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/datasources:/etc/grafana/datasources
      - ./grafana/dashboards:/etc/grafana/dashboards
      - ./grafana/setup.sh:/setup.sh
    entrypoint: /setup.sh
    environment:
      - GF_SECURITY_ADMIN_USER=${ADMIN_USER:-admin}
      - GF_SECURITY_ADMIN_PASSWORD=${ADMIN_PASSWORD:-admin}
      - GF_USERS_ALLOW_SIGN_UP=false
    restart: unless-stopped
    expose:
      - 3000
    networks:
      - monitor-net
      - dockernet
    labels:
      org.label-schema.group: "monitoring"

  caddy:
    image: stefanprodan/caddy
    container_name: caddy
    ports:
      - "3000:3000"
      - "9090:9090"
      - "9093:9093"
      - "9091:9091"
    volumes:
      - ./caddy/:/etc/caddy/
    environment:
      - ADMIN_USER=${ADMIN_USER:-admin}
      - ADMIN_PASSWORD=${ADMIN_PASSWORD:-admin}
    restart: unless-stopped
    networks:
      - monitor-net
      - dockernet
    labels:
      org.label-schema.group: "monitoring"

这是我的prometheus.yml文件。

global:
  scrape_interval:     15s
  evaluation_interval: 15s

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'docker-host-alpha'

# Load and evaluate rules in this file every 'evaluation_interval' seconds.
rule_files:
  - "alert.rules"

# A scrape configuration containing exactly one endpoint to scrape.
scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 10s
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'myapp'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['docker.for.win.localhost:32771']

  - job_name: 'myapp1'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['docker.for.win.host.internal:51626']

  - job_name: 'myapp2'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['docker.for.win.host.internal.localhost:51626']

  - job_name: 'myapp3'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['docker.for.win.host.localhost:51626']

  - job_name: 'myapp4'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['docker.for.win.localhost:51626']

  - job_name: 'myapp5'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['host.docker.internal:51626']

  - job_name: 'myapp6'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['host.docker.internal.localhost:51626']

  - job_name: 'myapp7'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['docker.for.win.localhost:51626']

  - job_name: 'myapp8'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['127.0.0.1:51626']

  - job_name: 'myapp9'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['localhost:51626']

  - job_name: 'myapp10'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['10.0.75.1:51626']

  - job_name: 'myapp12'
    scrape_interval: 10s 
    metrics_path: /metrics
    static_configs:
      - targets: ['localhost1:51626']

据我所知host.docker.internal应该引用我的主机IP并允许我访问本地应用程序,但事实并非如此。因此,然后我使用ipconfig(10.0.75.1地址)查找了我的docker NAT IP地址,但该方法也无效。

然后我尝试将localhost1的网络绑定到10.0.75.1。我尝试设置一个名为dockernet的桥接网络并以这种方式进行连接,但此操作无效。当我在Docker容器中启动应用程序时,可以通过“ docker.for.win.localhost:32771”访问该应用程序,但是该容器无法访问我的远程数据库,因此这就是为什么我需要它在本地运行。 Prometheus针对某些相应地址给出了以下响应:

Endpoint: Error
http://docker.for.win.localhost:32771/metrics:     UP
http://host.docker.internal:51626/metrics:     server returned HTTP status 400 Bad Request
http://docker.for.win.localhost:51626/metrics:     server returned HTTP status 400 Bad Request
http://host.docker.internal.localhost:51626/metrics:     Get http://host.docker.internal.localhost:51626/metrics: dial tcp: lookup host.docker.internal.localhost on 127.0.0.11:53: no such host
http://docker.for.win.host.internal.localhost:51626/metrics:     Get http://docker.for.win.host.internal.localhost:51626/metrics: dial tcp: lookup docker.for.win.host.internal.localhost on 127.0.0.11:53: no such host

我已经尝试了所有方法,但没有想法。谁能阐明一些想法?

3 个答案:

答案 0 :(得分:1)

对于我的作品来说

version: "3"

networks:
  sandbox:
    driver: bridge

services:

  prometheus:
    restart: always
    image: prom/prometheus:v2.3.2
    volumes: ["./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml"]
    ports: ["9090:9090"]
    extra_hosts: ["host.docker.internal:172.17.0.1"] # from Gateway bridge and added /etc/hosts
    networks: ["sandbox"]

  grafana:
     ....

答案 1 :(得分:0)

我有类似的问题。我在端口52562上的IIS Express上本地运行了自己的应用程序,容器内的prometheus显示http://docker.for.win.localhost:52562/metrics返回了400 BAD Request。 问题是IIS Express仅侦听localhost,因此我从以下位置在我的applicationhost.config中编辑绑定

<binding protocol="http" bindingInformation="*:52562:localhost" />

<binding protocol="http" bindingInformation="*:52562:" />

并重新启动IIS Express。

这解决了问题。

答案 2 :(得分:0)

您还可以检查Windows 10防火墙是否阻止了连接。

要完全禁用防火墙,请执行以下操作:

PrintWriter

要允许在特定端口上进行连接:

netsh advfirewall set allprofiles state off