title: Docker-compose could not translate host name "postgres"
body: <p>I'm fairly new to Docker.
  When I run <code>docker-compose up</code>, I would get the following error.</p>
  
  <pre><code>PG::ConnectionBad
  could not translate host name "postgres" to address: Name does not resolve
  </code></pre>
  
  <p>In the log, it says the following </p>
  
  <pre><code>postgres_1   | The database cluster will be initialized with locale "C".
  postgres_1   | The default database encoding has accordingly been set to "SQL_ASCII".
  postgres_1   | The default text search configuration will be set to "english".
  postgres_1   | 
  postgres_1   | Data page checksums are disabled.
  postgres_1   | 
  postgres_1   | initdb: directory "/var/lib/postgresql/data" exists but is not empty
  postgres_1   | If you want to create a new database system, either remove or empty
  postgres_1   | the directory "/var/lib/postgresql/data" or run initdb
  postgres_1   | with an argument other than "/var/lib/postgresql/data".
  </code></pre>
  
  <p>This is my docker-compose setup.</p>
  
  <pre><code># docker-compose.yml
  
  version: '3'
  services:
    postgres:
      image: postgres:9.6-alpine
      volumes:
        - postgresql-data:/var/lib/postgresql/data
      ports:
        - "5432:5432"
    app:
      build:
        context: .
      command: "bundle exec rails server -b 0.0.0.0"
      ports:
        - "3020:3000"
      depends_on:
        - postgres
  volumes:
    postgresql-data:
  </code></pre>
  
  <pre><code># Dockerfile
  
  FROM ruby:2.3.7-alpine
  
  ENV LANG C.UTF-8
  ENV RAILS_ENV=development
  
  RUN apk add --update --no-cache --virtual=builders \
        alpine-sdk build-base linux-headers ruby-dev zlib-dev postgresql-dev
  RUN apk add --update --no-cache \
        libc6-compat libc-dev zlib ruby-json tzdata yaml less curl postgresql
  RUN mkdir /app
  
  WORKDIR /app
  
  COPY Gemfile /app/Gemfile
  COPY Gemfile.lock /app/Gemfile.lock
  RUN bundle install
  COPY . /app
  </code></pre>
  
  <pre><code># database.yml
  default: &amp;default
    adapter: postgresql
    encoding: utf8
    host: postgres
    username: postgres
    password: 
    port: 5432
  
  development:
    &lt;&lt;: *default
    template: template0
    database: todo_development
  </code></pre>
  
  <p>What am I doing wrong here??</p>
  
  <p>If I change <code>host</code> from <code>postgres</code> to <code>localhost</code> in my <code>database.yml</code>, I get the following error.</p>
  
  <pre><code>Connection refused Is the server running on host “localhost” (127.0.0.1)
  </code></pre>