title: Docker container in docker-compose can't find other containers by hostname
body: <p>I am upgrading a docker-compose file from v1 to v3 and have a problem to link the containers together.</p>
  <p>Before the docker-compose.yml was this:</p>
  <pre><code>database:
    working_dir: /mnt
    volumes:
      - .:/mnt
    links:
     - postgresql_db
    ports:
      - '3000:3000'
    build: ./database
    entrypoint: python database/server.py
  
  postgresql_db:
    build: ./database
    ports:
      - '5432:5432'
  
  test_database:
    working_dir: /mnt
    volumes:
      - .:/mnt
    links:
      - test_postgresql_db
    ports:
      - '5053:5053'
    build: ./database/test
  
  test_postgresql_db:
    image: postgres:latest
    ports:
      - '5432:5432'
  </code></pre>
  <p>But links has been deprecated. Now containers should share a network (which they should do by default) and be able to find each other using hostnames (see: <a href="https://docs.docker.com/docker-cloud/apps/service-links/#dns-hostnames-vs-service-links" rel="nofollow noreferrer">here</a>).</p>
  <p>So I modified the file to:</p>
  <pre><code>version: '3'
  
  networks:
    dbnet:
      driver: bridge
  
  volumes:
    postgresql_data: {}
    postgresql_test_data: {}
  
  services:
    database:
      build: database
      environment:
        APPLICATION_DB_CONTAINER: db
        APPLICATION_POSTGRES_HOST: db
      working_dir: /mnt
      volumes:
        - .:/mnt
      networks:
        - dbnet
      ports:
        - '3000:3000'
      command: python database/server.py
  
    db:
      image: postgres:latest
      volumes:
        - postgresql_data:/var/lib/postgresql/data
      networks:
        - dbnet
      depends_on:
        - database
  
    test_database:
      build: database/test
      environment:
        APPLICATION_DB_CONTAINER: testdb
      working_dir: /mnt
      volumes:
        - .:/mnt
      command: python -m pytest --cov=database --cov-report term --cov-report html:htmlcov database/test/
  
    testdb:
      image: postgres:latest
      volumes:
        - postgresql_test_data:/var/lib/postgresql/data
  </code></pre>
  <p>The problem I have is that the database container can't find the db container by hostname and I get (my software tries to connect every second for 10 seconds and then stops):</p>
  <blockquote>
  <p>ERROR: Database postgresql://postgres:@db:5432/postgres NOT found (10
  attempts)</p>
  </blockquote>
  <p>I checked and the db container is up, if I use its IP address then it works but using the hostname does not.</p>
  <p><strong>How can I link the database and db container and make sure it finds it each time ?</strong></p>
  <p>Info: I run docker Version 18.06.0-ce-mac70 on MacOs.</p>
  <p>Thank you !</p>
  <p>EDIT: Tried to add this in the compose file for db, but did not change the problem.</p>
  <blockquote>
  <p>ports: '5432:5432'</p>
  <p>sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name &quot;db&quot; to address: Name or service not known
  database_1       |  (Background on this error at: <a href="http://sqlalche.me/e/e3q8" rel="nofollow noreferrer">http://sqlalche.me/e/e3q8</a>)</p>
  </blockquote>
  <p>I get this error from sqlalchemy that indicates that the database container does no know what db (or db-1) is and the file /etc/hosts does not contain any mention of another container</p>