title: Resolve containers using consul DNS on host
body: <p>Aim: Get a docker container to use the DNS provided by the host machine, which is a consul agent running in another container, to access services available via traefik reverse proxy.</p>
  
  <p>Setup
  Host machine: Ubuntu 16.04.2 LTS</p>
  
  <p>Registrator registers new containers to the consul agent, traefik is the reverse proxy to load balance the services and make them available. There is a general node app which returns "Hello World" when you hit the '/' path</p>
  
  <p>Container 1.</p>
  
  <pre><code>docker run -d --net=host consul agent -dev
  </code></pre>
  
  <p>Container 2.</p>
  
  <pre><code>docker run -d  --name=registrator --net=host --volume=/var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator:latest consul://localhost:8500
  </code></pre>
  
  <p>Container 3.</p>
  
  <pre><code>docker run -d --net=host -p 8089:8080 -p 80:80 --name=traefik -v ~/projects/traefix/docker.toml:/etc/traefik/traefik.toml traefik
  </code></pre>
  
  <p>Container 4.</p>
  
  <pre><code>docker run -P -d meep/node-web-app
  </code></pre>
  
  <p>Host machine using Network Manager</p>
  
  <p>The host machine is setup so any lookups for the consul TLD it will resolve to the consul docker container</p>
  
  <pre><code>/etc/NetworkManager/dnsmasq.d/10-consul
  server=/consul/127.0.0.1#8600
  </code></pre>
  
  <p>I have /etc/NetworkManager/dnsmasq.d/docker-bridge.conf with the following config which means it will listen for DNS requests on the docker network interface.</p>
  
  <pre><code>listen-address=172.17.0.1
  </code></pre>
  
  <p>Currently on my host machine I can do the following</p>
  
  <ol>
  <li>dig node-web-app.service.consul returns the ip address of 127.0.0.1</li>
  <li>curl <a href="http://node-web-app.service.consul/" rel="noreferrer">http://node-web-app.service.consul/</a> returns 'Hello World'</li>
  </ol>
  
  <p>So far so good everything working on the host.</p>
  
  <p>I boot up a container like</p>
  
  <pre><code>docker run --dns=172.17.0.1 -it joffotron/docker-net-tools
  </code></pre>
  
  <p>and run dig node-web-app.service.consul it returns 127.0.0.1, well at least the dns is partly working.
  Clearly if I now run curl <a href="http://node-web-app.service.consul/" rel="noreferrer">http://node-web-app.service.consul/</a> it will break as the dns lookup is pointing to 127.0.0.1, when it should point to 172.17.0.1</p>
  
  <p>Forgive me, I've not really work with docker + service discovery
  <strong>So what can I do so the containers DNS correctly point to 172.17.0.1</strong> ?</p>