将端口暴露给Docker容器(连接被拒绝)

bvpmtnay  于 2023-01-25  发布在  Docker
关注(0)|答案(1)|浏览(181)

我有一个使用autossh连接的服务器。当我在主机系统上运行curl http://localhost:9100/metrics时,我能够从服务器检索指标。我正在尝试将端口9100转发到我的docker-composite.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

为什么我不能进入集装箱内的港口?

1l5u6lss

1l5u6lss1#

假设我没有理解错,并且您希望从主机连接到端口9100上的容器,那么

expose:
- "9100"

部件是不必要的。您只需要像暴露9090一样暴露端口9100:

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

本部分:

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

也是不必要。我不知道你想达到什么目的,但是从Docker网络外部访问container和两个container之间的访问都不需要这样,从外部访问是通过上述端口转发实现的,两个container之间通过主机名的访问已经由Docker组建时自动创建的网络保证(用于docker-compose.yml中的所有服务)。

相关问题