Is there a better solution?
Absolutely! You do not need a host IP address for communication between containers. If you link containers in your docker-compose.yaml , you will have access to a number of environment variables that you can use to discover the IP addresses of your services.
Consider, for example, the configuration using docker with two containers: one with consul , and one with some service that you need to talk to the consul with.
consul: image: progrium/consul command: -server -bootstrap webserver: image: larsks/mini-httpd links: - consul
First, starting with consul with only -server -bootstrap , consul defines its own advertising address, for example:
consul_1 | ==> Consul agent running! consul_1 | Node name: 'f39ba7ef38ef' consul_1 | Datacenter: 'dc1' consul_1 | Server: true (bootstrap: true) consul_1 | Client Addr: 0.0.0.0 (HTTP: 8500, HTTPS: -1, DNS: 53, RPC: 8400) consul_1 | Cluster Addr: 172.17.0.4 (LAN: 8301, WAN: 8302) consul_1 | Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false consul_1 | Atlas: <disabled>
In the webserver container we find the following environment variables available for pid 1:
CONSUL_PORT=udp://172.17.0.4:53 CONSUL_PORT_8300_TCP_START=tcp://172.17.0.4:8300 CONSUL_PORT_8300_TCP_ADDR=172.17.0.4 CONSUL_PORT_8300_TCP_PROTO=tcp CONSUL_PORT_8300_TCP_PORT_START=8300 CONSUL_PORT_8300_UDP_END=udp://172.17.0.4:8302 CONSUL_PORT_8300_UDP_PORT_END=8302 CONSUL_PORT_53_UDP=udp://172.17.0.4:53 CONSUL_PORT_53_UDP_ADDR=172.17.0.4 CONSUL_PORT_53_UDP_PORT=53 CONSUL_PORT_53_UDP_PROTO=udp CONSUL_PORT_8300_TCP=tcp://172.17.0.4:8300 CONSUL_PORT_8300_TCP_PORT=8300 CONSUL_PORT_8301_TCP=tcp://172.17.0.4:8301 CONSUL_PORT_8301_TCP_ADDR=172.17.0.4 CONSUL_PORT_8301_TCP_PORT=8301 CONSUL_PORT_8301_TCP_PROTO=tcp CONSUL_PORT_8301_UDP=udp://172.17.0.4:8301 CONSUL_PORT_8301_UDP_ADDR=172.17.0.4 CONSUL_PORT_8301_UDP_PORT=8301 CONSUL_PORT_8301_UDP_PROTO=udp CONSUL_PORT_8302_TCP=tcp://172.17.0.4:8302 CONSUL_PORT_8302_TCP_ADDR=172.17.0.4 CONSUL_PORT_8302_TCP_PORT=8302 CONSUL_PORT_8302_TCP_PROTO=tcp CONSUL_PORT_8302_UDP=udp://172.17.0.4:8302 CONSUL_PORT_8302_UDP_ADDR=172.17.0.4 CONSUL_PORT_8302_UDP_PORT=8302 CONSUL_PORT_8302_UDP_PROTO=udp CONSUL_PORT_8400_TCP=tcp://172.17.0.4:8400 CONSUL_PORT_8400_TCP_ADDR=172.17.0.4 CONSUL_PORT_8400_TCP_PORT=8400 CONSUL_PORT_8400_TCP_PROTO=tcp CONSUL_PORT_8500_TCP=tcp://172.17.0.4:8500 CONSUL_PORT_8500_TCP_ADDR=172.17.0.4 CONSUL_PORT_8500_TCP_PORT=8500 CONSUL_PORT_8500_TCP_PROTO=tcp
There is a set of variables for each port of EXPOSE d using a consul image. For example, in the second image, we could interact with the consul's API by connecting to:
http://${CONSUL_PORT_8500_TCP_ADDR}:8500/
larsks Mar 18 '15 at 1:07 2015-03-18 01:07
source share