Title: Consul Template not finding service instances despite service being registered
I’m trying to create a dynamic Caddy configuration using Consul Template. The service is registered in Consul (verified via API), but the template can’t find the service instances.
Current Setup:
- Service is registered and visible:
$ consul catalog nodes -service=stopnship-api
Node ID Address DC
server1.stopnship.net 8c2ff822 <redacted> dc1
$ curl http://localhost:8500/v1/catalog/service/stopnship-api
[{
"ServiceID": "_nomad-task-xxx-group-stopnship-stopnship-api-stopnship-http",
"ServiceName": "stopnship-api",
"ServiceTags": ["api","api.stopnship.net"],
"ServiceAddress": "<redacted>",
"ServicePort": "<redacted>"
...
}]
Template:
template {
data = <<-EOT
{{ range services }}
{{ if .Tags | contains "api" }}
# Found service with api tag: {{ .Name }}
{{ range service .Name }}
{{ $svc := . }}
{{ range .Tags }}
{{ if ne . "api" }}
https://{{ . }} {
reverse_proxy * http://{{ $svc.Address }}:{{ $svc.Port }}
}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
EOT
destination = "local/caddy/Caddyfile"
}
The template finds services with the “api” tag (visible in the debug output) but service
function returns no instances, even though we can see the service is registered via API and consul commands.
Debug output shows:
# Found service with api tag: stopnship-api
# Looking up instances for service: stopnship-api
# No instances found for stopnship-api
Any ideas what might be causing this discrepancy between the catalog and the template’s service lookup?