如何在 WSL2 中连接 Grafana 和 Prometheus 容器 运行

How to connect on Grafana and Prometheus containers running inside WSL2

我 运行 Prometheus 和 Grafana 作为 WSL2 中的容器,我无法从 Windows 连接它们。我收到错误 connect ECONNREFUSED 127.0.0.1:9090(连接被拒绝)。

当我从 WSL2 内部访问它们时,一切正常。

docker-compose.yaml

version: '3.5'

services:
  prometheus:

    image: prom/prometheus:latest
    container_name: prometheus
    volumes:
      - ./prometheus/:/etc/prometheus/
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yaml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
    network_mode: "host"

  grafana:
    image: grafana/grafana:latest
    volumes:
      - grafana_data:/data
    network_mode: "host"

volumes:
  prometheus_data:
  grafana_data:

./prometheus/prometheus.yaml

global:
alerting:
rule_files:
scrape_configs:

当 WSL2 中的服务器 运行 位于 ::: 地址时,看起来有问题,例如来自 Grafana 的默认地址 - http.server address=[::]:3000.

如果主机名更改为 127.0.0.1,一切正常。

正在将服务器 IP 地址从 Grafana 和 Prometheus 更改为 127.0.0.1

在Prometheus上,需要添加这个命令--web.listen-address=127.0.0.1:9090

在Grafana上,需要添加这个环境变量GF_SERVER_HTTP_ADDR: "127.0.0.1"

docker-compose.yaml 主机名设置为 127.0.0.1

version: '3.5'

services:
  prometheus:

    image: prom/prometheus:latest
    container_name: prometheus
    volumes:
      - ./prometheus/:/etc/prometheus/
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yaml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.listen-address=127.0.0.1:9090'
    network_mode: "host"

  grafana:
    image: grafana/grafana:latest
    volumes:
      - grafana_data:/data
    environment:
      GF_SERVER_HTTP_ADDR: "127.0.0.1"
    network_mode: "host"

volumes:
  prometheus_data:
  grafana_data: