How does one service resolve the address of another service using docker compose, link hostname, and ports?

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.

I was under the impression that having the following in a docker-compose file would add ‘db’ to my container’s hostfile

  • Eval in docker-machine: terminal vs shell script
  • docker not unzipping my files
  • Connecting a Docker container to a network interface / device instead of an IP address
  • How does docker manage hashes for forking of docker images?
  • Debug django app running inside docker image, using pycharm debugger
  • Docker - curl returned “connection reset”
  • web:
        - db

    I was thinking that my web code could establish a db connection with something along the lines of

    db := sql.Open("postgres", "user=foo dbname=baz host=db")

    where hostname db exists in my web container’s /etc/hostfile and therefore resolves to some address that reaches the db container.

    My web application does not resolve to the address where my db is running, instead it resolves to an address ( and I cannot figure out where it has come from or how to fix.

    docker exec $WEB_CONTAINER_ID cat /etc/hosts   localhost
    ::1 localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters  6f52f9e78f00

    I was expecting to see an entry for db. Furthermore:

    docker exec $WEB_CONTAINER_ID nslookup db
    Name:      db
    Address 1: apiserver_postgres_1.apiserver_default

    What am I misunderstanding? How do I enable my web code to resolve my db address?

  • Docker container with Blazegraph Triple Store not working possibly due to networking
  • Running RC2 project on defined port
  • php 5.6 elasticache client on alpine 3.4
  • Save tables in Docker container
  • Does restarting a Docker container “remember” initial run arguments?
  • Mkbundle Mono Assembly binding redirection
  • One Solution collect form web for “How does one service resolve the address of another service using docker compose, link hostname, and ports?”

    This is no longer your classic docker 1.8 or less “link”, especially if you are using version 2 of docker-compose.yml file.

    As mentioned in issue 3002:

    version:”2″ creates a bridge network by default for your project. If you run docker network ls you’ll see a new bridged network where your project containers will be created. (which makes totally sense)

    Docker itself has removed these variables from links on any network except for the old bridge network.

    There is now an embedded DNS.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.