简体   繁体   English

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

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

I have a server that I've connected to using autossh.我有一台使用 autossh 连接到的服务器。 When I run curl http://localhost:9100/metrics on the host system, I am able to retrieve metrics from the server.当我在主机系统上运行 curl http://localhost:9100/metrics 时,我能够从服务器检索指标。 I am trying to forward port 9100 to the Prometheus Docker container in my docker-compose.yml file.我正在尝试将端口 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"

When I connect to the Prometheus container and run wget http://host.docker.internal:9100 , I receive the following error message:当我连接到 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

Why do i not have access to the port inside the container?为什么我无法访问容器内的端口?

Assuming I've understood you right and you want to connect to the container on port 9100 from your host, then the假设我没听错,你想从你的主机连接到端口 9100 上的容器,那么

expose:
- "9100"

part is unnecessary.部分是不必要的。 You just have to expose port 9100 the same as 9090:您只需要像公开 9090 一样公开端口 9100:

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

This part:这部分:

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

is unnecessary as well.也是不必要的。 I'm not sure what you try to achieve here, but neither the access to the container from outside the Docker.network nor the access between two containers needs that.我不确定您在这里尝试实现什么,但是从 Docker.network 外部访问容器和两个容器之间的访问都不需要。 The access from outside is achieved by port forwarding as described above.从外部访问是通过端口转发实现的,如上所述。 The access between two containers through hostname are already ensured by the.network that is automatically created at docker compose up (for all the services in the docker-compose.yml).两个容器之间通过主机名的访问已经由在 docker 自动创建的 .network 组成(对于 docker-compose.yml 中的所有服务)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM