bashnginxdockerconsulconsul-template

consul-template using Address not ServiceAddress in template


I am having a problem in that consul-template seems to be substituting the service "ServiceAddress" and not "Address" in my template and I wonder if anyone can tell me why.

From a bash session within my nginx container where consul-template is also running I can fetch the service definition from Consul with:

curl http://consul-server.service.consul:8500/v1/catalog/service/service1
[{"Node":"ip-172-31-24-202","Address":"172.31.24.202","ServiceID":"ip-172-31-24-202:service1:23141","ServiceName":"service1","ServiceTags":null,"ServiceAddress":"172.17.0.3","ServicePort":32809}]

My consul-template template file looks like:

{{range service "service1"}}server {{.Address}}:{{.Port}};

I would expect this to output the Address, and not the ServiceAddress for the service. However, the below happens:

consul-template -consul consul-server.service.consul:8500 -template "/var/templates/service1.conf.tmpl" -dry -once

server 172.17.0.3:32809

Solution

  • I've figured this out, .Address is part of the Consul response metadata, and not really part of the service metadata. This confused me because my consul Client is running on the same host.

    I changed the -ip argument to the Registrator service I was running to be the internal IP address of the host running docker (rather than the default which is the IP of the Docker container) and everything worked.