hendry
hendry

Reputation: 10813

Docker localhost IP

I'm running on my laptop various containers:

X1C3:~$ docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
4daccb82531d        prom/prometheus:latest   "/bin/prometheus -con"   12 hours ago        Up 12 hours         0.0.0.0:9090->9090/tcp   berserk_goldstine
32c2c31e0d5f        prom/blackbox-exporter   "/bin/go-run -config."   12 hours ago        Up 12 hours         0.0.0.0:9115->9115/tcp   goofy_wescoff
7490523a3bc7        prom/node-exporter       "/bin/go-run"            2 days ago          Up 2 days                                    sharp_albattani
61303633672b        prom/alertmanager        "/bin/go-run -config."   2 days ago          Up 2 days           0.0.0.0:9093->9093/tcp   goofy_kare
89ce4f49c426        grafana/grafana          "/usr/sbin/grafana-se"   2 days ago          Up 2 days           0.0.0.0:3000->3000/tcp   berserk_wozniak

And from at least one of those containers, prom/prometheus I need to communicate with the other containers.

For example here is a typical configuration:

global:
 scrape_interval: 10s
 evaluation_interval: 10s
scrape_configs:
 - job_name: 'prometheus'
   target_groups:
    - targets:
      - localhost:9090
      - 192.168.88.161:9100
 - job_name: 'blackbox'
   metrics_path: /probe
   params:
     module: [icmp]
     target: [8.8.8.8]
   target_groups:
     - targets:
       - 192.168.88.161:9115

I found that I needed to specify 192.168.88.161 since localhost wouldn't work. I guess because localhost is seem to be local to that container. However the annoying aspect of 192.168.88.161 is that it's only the IP when I am at home. My IP changes since I move around changes all the time. So how do deal with this issue?

I speculated using X1C3.local but I don't see that working without bonjour discovery working across containers (doubtful!)

Upvotes: 1

Views: 2243

Answers (2)

hendry
hendry

Reputation: 10813

What I have started doing now is using localhost in my /etc/prometheus.yml with docker run's --net=host option on all the services.

Upvotes: 1

VonC
VonC

Reputation: 1323203

If you are using docker-machine and docker 1.9, the new way to reference container would be through a discovery service, like, for instance consul, coupled with an inspector service like registrator.

The other similar approach is to use swarm to manage those services (still coupled with consul)

In all cases, that implies at least two more containers (one for the consul master, one for the inspection like a swarm master or a registrator).
See "What is the different between putting a separate service discovery and integrate it into the cluster machine in Docker Swarm".

The advantage over --link is that you can start your containers in any order, they can stop and be restarted, and they would still be visible one from another.
--link means the container you link to has to be started first, and it it stops and is recreated, the --link will not be valid anymore.

Also the discovery service approach is valid for localhost or multiple hosts. (--link only works locally, not accross hosts)

Upvotes: 1

Related Questions