You don't need to run a consul agent on every docker container, you can simply take advantage of consul by exposing it's DNS to your local. Following is not from a container but you will get the idea anyways as to what I am doing.
following is the command I am using to run my agent
consul agent -data-dir /var/lib/consul/ -config-dir /etc/consul.d/ -bind 10.X.X.X -dns-port 53 -join consul-master
Note: I have added a /etc/hosts entry for consul-master with it's IP and I have also added a nameserver for 127.0.0.1 in the /etc/resolv.conf file.
The directory /etc/consul.d/ holds my configuration file for the service. Following is an example:
{
"service": {
"name": "stackoverflow",
"tags": [
"example"
],
"port": 5000
}
}
Now once my consul agent is running, I can check on any host with consul agent (server/client) for the service via dig command or the http api request as follows:
curl http://stackoverflow.service.consul:80/api/v1/ping
{"success":true,"message":"pong"}
For DNS:
dig @127.0.0.1 -p 53 stackoverflow.service.consul
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.55.amzn1 <<>> @127.0.0.1 -p 53 tracker.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57167
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available
;; QUESTION SECTION:
;tracker.service.consul. IN A
;; ANSWER SECTION:
tracker.service.consul. 0 IN A X.X.X.X
;; Query time: 1 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Fri Jul 7 11:29:01 2017
;; MSG SIZE rcvd: 56
Hope that helps and gives a clear idea of it