-
Notifications
You must be signed in to change notification settings - Fork 670
make Java InetAddress.getLocalHost() (similar to hostname -i) return the weave IP address #68
Comments
Why? |
More detail (I'm posting this on behalf of someone who approached via email) I'm using Consul for service registration and discovery. While registering services they default to the address provided by the host which is usually in the "172.x." range given by Docker. I can manually switch that to advertise the "10.0.x"-range by querying the interfaces directly with:
Also I can do the same with Cassandra and with sed insert that IP to the cassandra.yaml so that they can talk with each other, also across nodes. Inside a single host having them use the "172.x" range is of course okay, but it won't fly across the Weave network. I would much rather have both Consul registration and Cassandra report the IP of the Weave-interface directly, they both most probably use the address reported by "hostname -i". I got around that with pipework and our own DNS implementation with telling Ubuntu to use DNS before hosts-file, but that is quite kludgy and in this case wouldn't even work as Consul provides the DNS for all the hosts and the hosts register themselves instead of me registering them outside of the container where I can declare the IP for them. Basically either with metric-information or by otherwise ordering the interfaces I'd like to have Weave return the ethwe IP with "hostname -i". Both IPs are reported with "hostname -I". I think changing the metrics in running containers is prohibited (at least I get an error when I try), so I'm out of ideas there. |
Would disabling the docker networking work? ( |
Interestingly, no. Even though that address doesn't work, it is still returned:
Looks like
Changing the address listed in /etc/hosts has the desired effect on (See also moby/moby#1951, on the subject of editing /etc/hosts inside a container) |
On my ubuntu machine, having two entries for my hostname in /etc/hosts causes What do the likes of Consul and Cassandra actually do? Do they literally take the output of |
OP here. When I was using pipework (by jpetazzo) and a custom DNS server implementation, I had 'hostname -i' return the DNS server given address for the host by altering the order of lookup in /etc/nsswitch.conf. In that case the interface was created after the container was started and also added to the DNS server with the hostname so that DNS lookup returned the IP I gave to the container. When the IP address is given to the container by Weave at start-up for the ethwe-interface and Consul provides the DNS, it seems that Consul registers the host with the IP address that the hosts-file provides for localhost. This also seems to be what 'hostname -i' returns. In Cassandra documentation it is mentioned that if the bind address is left empty, it uses InetAddress.getLocalHost() to resolve.As described above, I can override and parse the IP address from ethwe interface, but things get quite kludgy. I try to make the applications in the container as agnostic about the environment as possible and in this case the container thinking it has the Weave provided IP address (instead of the Docker provided node local one) would make sense, especially if I connect services from multiple nodes together over Weave. One solution could be that Weave (maybe with a switch) inserts the defined IP address to the hosts file at start-time so that it is defined before the docker-written one if that is possible. I tried modifying the metrics for the interfaces so that I could see if ethwe would be preferred, but that was prohibited in a running container. Probably /etc/hosts would still provide the address, though. |
OK, I see the code calling getLocalHost(). I have renamed this issue to more accurately reflect what you want. To @rade 's point, the Java implementation takes the first address returned after querying all configured name services. |
@bboreham you wrote
but I cannot see how that could be the case - when docker networking is disabled then it doesn't even assign an IP. Here's what I'm getting...
This obviously doesn't solve the problem, but means that @SirIle's suggestion of inserting the weave IP at the start of /etc/hosts strikes me as a sensible solution. Are there any common circumstances under which that is the wrong thing to do? I'd rather avoid introducing a switch. Also, what does Java's |
It returns the first one, by my reading of the Java source code. Can't comment on why your experiment gave a different result to mine; maybe Docker has changed. |
See #47 |
As of docker 1.4.1 you can add entries to /etc/hosts with |
weave run with " --add-host=name:IP --net=none -h name " will solve the problem! ip=10.2.1.26;weave run --with-dns $ip/24 --net=none --add-host=a1:$ip -ti -h a1 ubuntu |
|
Also, if you are running with |
I also tried prepending an entry and using docker links at the same time, to see whether docker's /etc/hosts updating stomps on the file. It doesn't; it just updated the entry for the link and left my modifications alone. The only gotcha I can see is that because the file is mounted, there is no atomic way to update it. So, anyway, strawman proposal: make We should probably also deal with dynamic add/remove via |
The trouble is that we have no easy way to do that from the script since we somehow need to reach into the container's filesystem. |
Running Mesos in Docker with Weave & DNS is also problematic due to this, as it looks up it's hostname and registers own cluster membership with the IP address it finds, which turns out to be Docker IP, of course... Just for the record. |
At present, a container started in the way described in the weave README will have two IP addresses: one assigned by Docker and one assigned through weave. So
hostname -I
will return something like:however,
hostname -i
returns the Docker-assigned IP:I would prefer
hostname -i
to return the weave addressThe text was updated successfully, but these errors were encountered: