无法从docker容器访问主机

问题描述 投票:0回答:2

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

设置prometheus和grafana堆栈以监控在localhost上运行的服务的度量标准。这是我的docker撰写文件。

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

我已经尝试了所有的东西,但是我没有想法。任何人都能解释一下吗?

docker prometheus
2个回答
0
投票

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

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

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

并重新启动IIS Express。

这解决了这个问题。


0
投票

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

要完全禁用防火墙:

netsh advfirewall set allprofiles state off

要允许特定端口上的连接:

New-NetFirewallRule -Protocol TCP -LocalPort 44369 -Direction Inbound -Action Allow -DisplayName "Allow network TCP on port 44369"
© www.soinside.com 2019 - 2024. All rights reserved.