title: Docker (rails, postgresql) hanging when started, rather than connecting to DB
body: <p>I'm new to docker, and trying to workout why my Docker setup is hanging and not connecting like I expect it to.</p>
  
  <p>I'm running</p>
  
  <ul>
  <li>Docker version 18.09.2, build 6247962</li>
  <li>docker-compose version 1.23.2, build 1110ad01</li>
  <li>OSX 10.14.5</li>
  </ul>
  
  <p>My setup is based on this <a href="https://gist.github.com/oinak/b923cd1378ea7daea2e8610f8b796eed" rel="nofollow noreferrer">Gist</a> that I found.</p>
  
  <p>I've reduced it somewhat, to better demonstrate the issue.</p>
  
  <p><em>Dockerfile</em></p>
  
  <pre><code>FROM ruby:2.4
  
  ARG DEBIAN_FRONTEND=noninteractive
  
  RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main" &gt;&gt; /etc/apt/sources.list.d/postgeresql.list \
   &amp;&amp; wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
   &amp;&amp; apt-get update                                     \
   &amp;&amp; apt-get update                                     \
   &amp;&amp; apt-get install -y --no-install-recommends apt-utils \
   &amp;&amp; apt-get install -y build-essential \
   &amp;&amp; apt-get install -y nodejs \
   &amp;&amp; apt-get install -y --no-install-recommends         \
     postgresql-client-9.6 pv ack-grep ccze unp htop vim \
   &amp;&amp; apt-get install -y libxml2-dev libxslt1-dev \
   &amp;&amp; rm -rf /var/lib/apt/lists/*                        \
   &amp;&amp; apt-get purge -y --auto-remove
  
  # Set environment
  ENV APP_HOME /usr/src/app
  ENV BUNDLER_VERSION 2.0.2
  
  # Setup bundler
  RUN gem install bundler -v $BUNDLER_VERSION
  
  WORKDIR $APP_HOME
  
  EXPOSE 7051
  
  CMD ["bundle", "exec", "puma", "-p", "7051", "-C", "config/puma.rb"]
  </code></pre>
  
  <p><em>docker_compose.yml</em></p>
  
  <pre><code>version: '3.1'
  services:
    app: &amp;app_base
      build: .
      working_dir: /usr/src/app
      volumes:
        - .:/usr/src/app
        # to be able to forward ssh-agent to github through capistrano (bundle on server)
        - "~/.ssh/id_rsa:/root/.ssh/id_rsa"
        - $SSH_AUTH_SOCK:$SSH_AUTH_SOCK
      environment: &amp;app_environment
        # to keep bundle effect between container restarts (without rebuild):
        BUNDLE_PATH: /usr/src/app/.bundle
        BUNDLE_APP_CONFIG: /usr/src/app/.bundle
        DATABASE_HOST: db
        SSH_AUTH_SOCK: # this left empty copies from outside env
      env_file: '.env'
      ports:
        - "7051:7051"
      depends_on:
        - db
  
    db:
      image: postgres:9.5.17
      ports:
        - "5432:5432"
      environment:
        POSTGRES_DB: my_project_development
        POSTGRES_USER: root
        POSTGRES_PASSWORD: root
  </code></pre>
  
  <p><em>config/database.yml</em></p>
  
  <pre><code>development:
    adapter: postgresql
    encoding: unicode
    pool: 5
    database: my_project_development
    username: root
    password: root
    host: db
  </code></pre>
  
  <p><em>config/puma.rb</em></p>
  
  <pre><code>threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
  threads threads_count, threads_count
  
  # Specifies the `port` that Puma will listen on to receive requests, default is 3000.
  #
  port        ENV.fetch("PORT") { 7051 }
  
  # Specifies the `environment` that Puma will run in.
  #
  environment ENV.fetch("RAILS_ENV") { "development" }
  
  # Allow puma to be restarted by `rails restart` command.
  plugin :tmp_restart
  
  </code></pre>
  
  <p>So what I'm doing is:</p>
  
  <ol>
  <li>Running <code>docker-compose build</code> to first build the images &amp; containers</li>
  <li>Running <code>docker-compose run --rm app bundle install</code> to install the gems</li>
  <li>Running <code>docker-compose run --rm app bundle exec rake db:create db:migrate db:seed</code> to create/migrate/seed the database</li>
  </ol>
  
  <p>Step 3. is the step I am stuck on.  It just hangs there with no feedback:</p>
  
  <pre><code>docker-compose run --rm app bundle exec rake db:create db:migrate db:seed
  Starting my_project_db_1 ... done
  
  </code></pre>
  
  <p>I know the database is running, as I can connect to it locally.</p>
  
  <p>I can also log into the <code>app</code> container, and connect via <code>psql</code>, so I know that the <code>app</code> container can talk to the <code>db</code> container:</p>
  
  <pre><code>docker exec -it f6d6edadaed4 /bin/bash                                                                                       (52s 699ms)
  root@f6d6edadaed4:/usr/src/app# psql "postgresql://root:root@db:5432/my_project_development"
  psql (9.6.14, server 9.5.17)
  Type "help" for help.
  
  my_project_development=# \dt
  No relations found.
  </code></pre>
  
  <p>If I try to boot the app with <code>docker-compose up</code>, then it also just hangs:</p>
  
  <pre><code>app_1  | Puma starting in single mode...
  app_1  | * Version 3.11.4 (ruby 2.4.6-p354), codename: Love Song
  app_1  | * Min threads: 5, max threads: 5
  app_1  | * Environment: ci
  </code></pre>
  
  <p>I.e. puma would normally show a 'listening' message once connected:</p>
  
  <pre><code>* Listening on tcp://0.0.0.0:7051
  Use Ctrl-C to stop
  </code></pre>
  
  <p>But it's not getting to that point, it just hangs.</p>
  
  <p>What could be going on?  Why can't my Rails container just connect to the PostgreSQL container and have puma boot normally?</p>
  
  <h2>MORE INFORMATION:</h2>
  
  <p>I've learn't now, if I wait 10+ minutes, it does eventually boot!</p>
  
  <p>During that 10 mins, my CPU fans are spinning like crazy, so it's really thinking about something.</p>
  
  <p>But when it finishes, the CPU fans shut off, and puma has booted and I can access it locally at <a href="http://127.0.0.1:7051" rel="nofollow noreferrer">http://127.0.0.1:7051</a> like I would expect.</p>
  
  <p>Why would it be so slow to startup?  My machine is otherwise pretty fast.</p>