title: How does one service resolve the address of another service using docker compose, link hostname, and ports?
body: <p>I have read several SO questions revolving the same question, but due to insufficient points, I cannot comment on existing questions. Therefore, I must spawn my own question thread regarding docker-compose and links.</p>
  
  <p>I was under the impression that having the following in a docker-compose file would add 'db' to my container's hostfile</p>
  
  <pre><code>web:
    links:
      - db
  </code></pre>
  
  <p>I was thinking that my web code could establish a db connection with something along the lines of</p>
  
  <pre><code>db := sql.Open("postgres", "user=foo dbname=baz host=db")
  </code></pre>
  
  <p>where hostname <em>db</em> exists in my web container's /etc/hostfile and therefore resolves to some address that reaches the db container.</p>
  
  <p>My web application does not resolve to the address where my db is running, instead it resolves to an address (172.19.0.3) and I cannot figure out where it has come from or how to fix.</p>
  
  <pre><code>docker exec $WEB_CONTAINER_ID cat /etc/hosts
  127.0.0.1   localhost
  ::1 localhost ip6-localhost ip6-loopback
  fe00::0 ip6-localnet
  ff00::0 ip6-mcastprefix
  ff02::1 ip6-allnodes
  ff02::2 ip6-allrouters
  172.19.0.2  6f52f9e78f00
  </code></pre>
  
  <p>I was expecting to see an entry for db. Furthermore:</p>
  
  <pre><code>docker exec $WEB_CONTAINER_ID nslookup db
  Name:      db
  Address 1: 172.19.0.3 apiserver_postgres_1.apiserver_default
  </code></pre>
  
  <p>What am I misunderstanding? How do I enable my web code to resolve my db address?</p>