I have a bridge network in my compose configuration where some containers are set with fixed IP addresses. This works well in that they can see services running on each other etc.
However, when I try and access 127.0.0.1 and a known port that listening from one of these containers, I get connection refused:
nc 127.0.0.1 6379
(UNKNOWN) [127.0.0.1] 6379 (?) : Connection refused
ip route shows:
default via 172.19.2.1 dev eth0
172.19.2.0/24 dev eth0 proto kernel scope link src 172.19.2.8
On host, service is running as shown:
tcp 0 0 localhost:6379 : LISTEN
I need to run some containers on the host network due to redis/sentinel failover detection but not all as I’ll get port conflicts.
From within a container, 127.0.0.1 is always that container itself.
(I guess later on you mention running some containers as --net host, and there, 127.0.0.1 is the host. But you need to find an IP address for the host and pass it into other-network containers as “the host address” for this sort of use case. In some environments there’s a “normal” IP address you can use, maybe 172.17.0.1 on native Linux and 192.168.99.100 on Docker Toolbox, but there’s not anything fixed for Docker for Mac, and the fixed address may not work on a non-default bridge network.)
(Think of this problem as each container being a physically separate server, and the host is your router.)
If you’re setting up a bridge network anyways, then you should use the built-in Docker DNS service and refer to other containers by their names. Then you won’t be dependent on fixed IP addresses for anything. (They’re a hassle in pretty much any environment.)
Thanks for replying - I see your point about localhost being local to each container.
I can’t use container names / hostnames as Redis sentinels update the Redis server config directly with IP addresses.
Unless these are running on the host network, they do not listen to each other correctly (there’s a note about this on the Redis page http://redis.io/topics/sentinel).
I did try binding the sentinels and Redis servers to by custom bridge gateway address and I could then access this from my containers in the custom bridge, but then sentinels failed to promote a server.
So it seems I need to run Redis / sentinels in a host network other than local host, or everything runs on localhost. I’ll see if I can use host network and specify some fixed IP addresses. Not currently sure where to set these though in compose yaml.
I think I have a solution now. Instead of binding to the host network lo interface on the Redis container, I now bind to the single static IP address I have for eth0. This is routeable from all bridge containers and Redis is happy as it is a host interface.
Thanks for your help - it was an important point about localhost!