Some (many?) containers must know at docker run time, what is the hostname/port of the host they are running on.
Apps that explicitly expose their IP for other apps for example. Many clustered applications / DBs do this, as well as Consul-registrator, Consul-template etc.
When using Swarm, one does not know the target hostname/IP because the target host is unknown by design.
The needed data cannot be provided and so, Swarm cannot be used to run such containers.
It may be tempting for one to view this as a "Docker" daemon issue: In a similar fashion as Swarm does, one may use a Docker client to connect to a Docker daemon and attempt to run such a container.
One will not be able to use: docker run ... -e my_ip=$(hostname -i) ...
However, that is not the case. When a user or automated system connect to a remote docker daemon, they possess the information of what host is being targeted.
Not so when using Swarm. Swarm, and only Swarm holds the information of what the target host is, and Swarm is not in a sharing mood.
Overview
Some (many?) containers must know at docker run time, what is the hostname/port of the host they are running on.
Apps that explicitly expose their IP for other apps for example. Many clustered applications / DBs do this, as well as Consul-registrator, Consul-template etc.
example:
Problem
When using Swarm, one does not know the target hostname/IP because the target host is unknown by design.
The needed data cannot be provided and so, Swarm cannot be used to run such containers.
It may be tempting for one to view this as a "Docker" daemon issue: In a similar fashion as Swarm does, one may use a Docker client to connect to a Docker daemon and attempt to run such a container. One will not be able to use:
docker run ... -e my_ip=$(hostname -i) ...
However, that is not the case. When a user or automated system connect to a remote docker daemon, they possess the information of what host is being targeted.
Not so when using Swarm. Swarm, and only Swarm holds the information of what the target host is, and Swarm is not in a sharing mood.
Solution
Add Swarm placeholders:
swarm_target_ip
,swarm_target_host
.When finding these in the
swarm run
command, Swarm will replace these with the needed data.pros: simple, works. cons: ugly. alternatives: ???
code example for the fix: (works great)
In cluster/swarm/cluster.go