Docker Community Forums

Share and learn in the Docker community.

Name vs hostname in swarm mode


(Lachele) #1

Issue type: behavior in swarm mode vs regular mode.

OS: This is a three-node swarm, with all three nodes being managers. for two nodes, debian 9.4 and docker 17.12.1-ce, build 7390fc6; the third node is debian 8.10 and 17.05.0-ce, build 89658be

To reproduce:

  1. have two containers/services that communicate via the network (e.g., web server and database).

  2. want to make two stacks/sets of these that can coexist on the same multi-user machine or in the same swarm.

  3. in regular mode, give both webservers the same hostname and network-alias and both databases the same hostname and network-alias, but give all four different docker names. Put one webserver and database in one network; put the other pair in another network. Note that the same internal code can be used in each stack to have webserver talk to database because the network-aliases are the same in each network.

  4. try doing this in swarm mode with services rather than containers. it won’t work because swarm uses only the name as the network-addressing-label. the use of hostname is unclear. you can only have both sets running if the names are all different (which is just fine), but now you have to tweak your code for each set because the hostname isn’t used for the machines to find each other.

Here is the information from the steps-to-reproduce said in different language for possible clarification:

When using the regular docker containers, the containers found each other by network-alias (which we set to the hostname), and name could be just a way for docker (and humans) to keep all the containers separate. This is useful if you want two environments (say, development and production) running on the same machine. We rely a lot on that ability. We put each set of containers in a separate subnet so that each set behaves internally in exactly the same way. The only difference is that docker has different names for the containers (and a different subnet). The containers ignore their names and networks and reference each other by hostname/network-alias.

But, in swarm mode, the services find each other by name and seem to ignore the hostname, and there is no network-alias. Is that the way it’s supposed to be?

We need to have two stacks, or “combs” (to use a hive reference) that are essentially identical within the one swarm (one development ‘comb’ and one for production). But, we can’t have the names be the same or docker won’t let us have two in the same swarm. And, if we change the names, we also have to make our code more complex by having it respond to its environment.

Is there some other way this is supposed to happen? It was really nice with the containers to be able to have an in-network referencing method that was distinct from the overall docker world.


Docker Engine Swarm mode and hostnames