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

  • automatically start /etc/init script in IBM Bluemix container
  • Kubernetes: managing application run environments
  • Docker on the mac separates internal and external file ownerships; not so on linux
  • auto deploy to docker from nexus repo when maven project version changed
  • Docker fails to install php mysql extension
  • Changes to css, js, and static files not reflected on nginx docker container
  • 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?

  • What is the difference between docker-compose ports vs expose
  • Minikube: how to ssh into the VM?
  • Calling a service running in a docker container from an angular2 app
  • Docker run/star/exec?
  • Is it possible to access a hardware device with a docker image under Windows?
  • Getting docker working in daemon mode for a tahoe-lafs storage node?
  • 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.