0

I have a server that I've connected to using autossh. When I run curl http://localhost:9100/metrics on the host system, I am able to retrieve metrics from the server. I am trying to forward port 9100 to the Prometheus Docker container in my docker-compose.yml file.

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
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?

  • `expose:` does basically nothing, and you can just delete that block. Do you need to publish the port using `ports:`, the same as the main Prometheus port? – David Maze Jan 22 '23 at 15:26
  • @DavidMaze thanks for your answer. No, I only need access to port 9100 from the docker host in the prometheus container. So, the Prometheus container must be able to reach Docker host port 9100, which is binded on localhost on docker host. – Marvin Ludwig Jan 22 '23 at 15:31
  • Oh, you're asking, [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – David Maze Jan 22 '23 at 15:34

1 Answers1

1

Assuming I've understood you right and you want to connect to the container on port 9100 from your host, then the

expose:
- "9100"

part is unnecessary. You just have to expose port 9100 the same as 9090:

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. 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).