Assign hostname to tasks/"created services" in Native Swarm Docker 1.12

Docker 1.12

Problem: I am trying to dockerize distributed database - create a cluster using multi-host. The node clusters communicate to each other using IP address/hostnames (application that is dockerized uses zookeeper internally) and has to be provided during startup time.

On single host, in docker environment, I can create cluster by simply assigning names (–name) to each container and pass in the container names (as they are known) as environment variable to container with “docker run”. This is pretty simple and works great.

I want to replicate same thing where the docker tasks can run on different machines and communicate to each other using the hostnames or IP assigned. For the same I have looked at cluster creationg using Docker Engine’s swarm introduced in 1.12. Spinning up a cluster in 1.12 is really simple though one thing I was not able to figure out was if I can associate a name to a task during “docker service create” - assign name to docker instance created by service that starts running in swarm and not the swarm node? If so, how?

Thanks in advance

Rather than having the user supply the names, I’d rather see the “docker service create...” add an implicit “--net-alias=<service name>-<n>” to each replica it runs ("<n>" being the task sequence number, and have Docker pass this alias in an ENV (e.g., TASK_NETWORK_ALIAS=“myservice-1”). Since these tasks would be sequentially numbered, any client using the service could specify “myservice-1” for direct access to the first task that runs or “myservice-3” for the third. The name $TASK_NETWORK_ALIAS should also resolve from within the container.

Docker does not support --net-alias=<service name>-<n>. You have to create one service for each replica, and you could name service like <service name>-<n>.

There are 2 good discussions/examples about how to setup distributed database on Docker swarm. How to handle database replication and get zookeeper running in docker swarm mode.