I want to create a Docker Swarm Service in a Centos environment with 4 replicas- which are created on multiple docker hosts.
In the service tasks created, I want to use the hostname of the respective docker host on which the task is created.
Can someone please let me know how to access the respective docker hostname from inside the service task/container.
By using docker templates, I could get the Node.Id in the swarm {{.Node.ID}}. From the node.id, I can get the node.name by the unix command
docker node ls|grep {{.Node.ID}}|awk ‘{print $2}’`
How can I input this command to the environment variable ?
for eg.,
docker service create --name xxservice -e xxenv_var=docker node ls|grep {{.Node.ID}}|awk ‘{print $2}’
(this is not working)
Yes, that works for me too. But, I don’t need ‘nodeid’. I need node name.
And that’s what I’m trying using below command line.
xxenv_var=$(docker node ls | grep ${nodeid} | awk ‘{print $2}’)
So instead of the below command
docker service create --name yy --env nodeid={{.Node.ID}} --detach nginx hh7uko0b9ms7sl6mbrof0wyd3
I’m trying the below ------>>>>
docker service create --name yy --env nodename=$(docker node ls | grep ${nodeid} | awk ‘{print $2}’) --detach nginx hh7uko0b9ms7sl6mbrof0wyd3
So, as you’ve connected to the container, and have $nodeid populated with the below value
root@72c1eac4f7d0:/# echo $nodeid
t6m94lg5kl7l38orgwb9f6cfw
I would like to have nodename for the respective nodeid inside the container, which is not working for me.
Yes, I tried that. But the volume contains only one hostname of the machine from where the service is created.
But I’m creating service with --replicas=4, where 4 containers are created on 4 different machines/nodes.
So, the volume wouldn’t be useful because it has only one hostname, not each node name of the respective container.
Hi, i have tested in my environment and binding the file /etc/hostname i can get mapped in each replica the node hostname where the replica is running:
"ContainerSpec": {
"Image": "nginx:latest",
"Mounts": [
{
"Type": "bind",
"Source": "/etc/hostname",
"Target": "/etc/hostname"
}
],
vidaxl@QAS-RANCHER-HOST-01:~$ sudo docker service ps testdaniel
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
e0i4f5z0szia testdaniel.1 nginx:latest QAS-RANCHER-HOST-01 Running Running 4 minutes ago
4l5cip628ck8 testdaniel.2 nginx:latest QAS-RANCHER-HOST-02 Running Running 4 minutes ago
vidaxl@QAS-RANCHER-HOST-01:~$ sudo docker ps | grep testda
ff889365bd26 nginx:latest "nginx -g 'daemon ..." 5 minutes ago Up 5 minutes 80/tcp testdaniel.1.e0i4f5z0sziaw5aki7x7fqi2n
vidaxl@QAS-RANCHER-HOST-01:~$ sudo docker exec -it ff889365bd26 bash
root@ff889365bd26:/# cat /etc/hostname
QAS-RANCHER-HOST-01
root@ff889365bd26:/#
And the other replica running in other host:
vidaxl@QAS-RANCHER-HOST-02:~$ sudo docker service ps testdaniel
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
e0i4f5z0szia testdaniel.1 nginx:latest QAS-RANCHER-HOST-01 Running Running 6 minutes ago
4l5cip628ck8 testdaniel.2 nginx:latest QAS-RANCHER-HOST-02 Running Running 6 minutes ago
vidaxl@QAS-RANCHER-HOST-02:~$ sudo docker ps | grep testdani
ad1876faba5a nginx:latest "nginx -g 'daemon ..." 6 minutes ago Up 6 minutes 80/tcp testdaniel.2.4l5cip628ck80rpoc3ezemfuh
vidaxl@QAS-RANCHER-HOST-02:~$ sudo docker exec -it ad1876faba5a bash
root@ad1876faba5a:/# cat /etc/hostname
**QAS-RANCHER-HOST-02**
root@ad1876faba5a:/#
Since i’m using binding volumes, docker daemon will mount the local /etc/hostname in each replica.
So each replica will have mounted the hostname file of the node where it is running:
You’re absolutely right!
I could get the node hostname in each replica container. I tried this before also, but somehow I didn’t make it work. Now its working as I needed.
Thanks a lot !
What would really be nice is if you could get the hostname to be the taskname, so in your containers example above the hostnames would be testdaniel.1 and testdaniel.2. Anyone have ideas about how to accomplish this?