title: Connect to postgresql container from another container (Docker)
body: <p>I have am trying to follow this <a href="https://docs.docker.com/compose/startup-order/" rel="noreferrer">tutorial</a> and set up a postgresql container.</p>
  
  <p>I have the following script:</p>
  
  <pre><code>#!/bin/bash
  # wait-for-postgres.sh
  
  set -e
  
  host="$1"
  shift
  cmd="$@"
  
  until psql -h "$host" -U "postgres" -c '\l'; do
    &gt;&amp;2 echo "Postgres is unavailable - sleeping"
    sleep 1
  done
  
  &gt;&amp;2 echo "Postgres is up - executing command"
  exec $cmd
  </code></pre>
  
  <p>And the following <code>docker-compose.yml</code>:</p>
  
  <pre><code>version: '2'
  services:
    server:
      build: .
      ports:
        - 3030:3030
      depends_on:
        - database
      command: ["./setup/wait-for-postgres.sh", "localhost:5432", "--", "node", "src"]
    database:
      image: postgres
      environment:
        - "POSTGRES_USER=postgres"
        - "POSTGRES_PASSWORD=postgres"
        - "POSTGRES_DB=tide_server"
      ports:
        - 5432:5432
  </code></pre>
  
  <p>The problem is that when I run <code>docker-compose up</code> I get the following error:</p>
  
  <pre><code>server_1    | Postgres is unavailable - sleeping
  server_1    | psql: could not translate host name "192.168.64.2:5432" to address: Name or servi
  ce not known
  server_1    | Postgres is unavailable - sleeping
  server_1    | psql: could not translate host name "192.168.64.2:5432" to address: Name or servi
  ce not known
  server_1    | Postgres is unavailable - sleeping
  server_1    | psql: could not translate host name "192.168.64.2:5432" to address: Name or servi
  ce not known
  </code></pre>
  
  <p>Now I have tried setting the host as <code>database</code>, <code>localhost</code>, <code>0.0.0.0</code>, and even the containers IP but nothing works, I have no idea what it should be or how to debug it, I am not 100% sure how <code>docker-compose</code> links the containers.</p>