簡體   English   中英

將端口暴露給 docker 容器(連接被拒絕)

[英]Expose port to docker container (Connection refused)

我有一台使用 autossh 連接到的服務器。 當我在主機系統上運行 curl http://localhost:9100/metrics 時,我能夠從服務器檢索指標。 我正在嘗試將端口 9100 轉發到我的 docker-compose.yml 文件中的 Prometheus Docker 容器。

services:
  prometheus:
  image: prom/prometheus:latest
  container_name: prometheus
  ports:
    - "9090:9090"
  expose:
    - "9100" # Node Exporter
  volumes:
    - $DOCKERDIR/appdata/prometheus/config:/etc/prometheus
    - $DOCKERDIR/appdata/prometheus/data:/prometheus
  restart: unless-stopped
  command:
    - "--config.file=/etc/prometheus/prometheus.yml"
  extra_hosts:
    - "host.docker.internal:host-gateway"

當我連接到 Prometheus 容器並運行 wget http://host.docker.internal:9100時,我收到以下錯誤消息:

/prometheus $ wget http://host.docker.internal:9100
Connecting to host.docker.internal:9100 (172.17.0.1:9100)
wget: can't connect to remote host (172.17.0.1): Connection refused

為什么我無法訪問容器內的端口?

假設我沒聽錯,你想從你的主機連接到端口 9100 上的容器,那么

expose:
- "9100"

部分是不必要的。 您只需要像公開 9090 一樣公開端口 9100:

ports:
- "9090:9090"
- "9100:9100"

這部分:

extra_hosts:
- "host.docker.internal:host-gateway"

也是不必要的。 我不確定您在這里嘗試實現什么,但是從 Docker.network 外部訪問容器和兩個容器之間的訪問都不需要。 從外部訪問是通過端口轉發實現的,如上所述。 兩個容器之間通過主機名的訪問已經由在 docker 自動創建的 .network 組成(對於 docker-compose.yml 中的所有服務)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM