dockerdocker-composeprometheusmetricsconnection-refused

Expose port to docker container (Connection refused)


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?


Solution

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