title: How to connect to a PostgreSQL container with Rails
body: <p>I have a Docker-file which is declaring my Ruby on Rails application with nginx and a PostgreSQL database.</p>
  
  <p>I'm struggling to understand how  my Rails application connects to my PostgreSQL container, because it doesn't connect at all.</p>
  
  <p>I built the Docker-file linking the images:</p>
  
  <pre><code>web:
    build: .
    command: bundle exec puma -C config/puma.rb
    volumes:
      - /tmp:/tmp
      # - /log:/data
      - .:/my_project_folder
    links:
      - nginx
      - db
  
  nginx:
    build: ./nginx
    volumes:
      - /tmp:/tmp
    ports:
      - 80:80
  
  db:
    image: postgres
    environment:
        POSTGRES_PASSWORD: "Postgres2019!"
    ports:
      - "15432:5432"
    volumes:
      - /Users/fred/Documents/Postgres/data:/var/lib/postgresql/data
  </code></pre>
  
  <p>and configured my database.yml like:</p>
  
  <pre><code>default: &amp;default
    adapter: postgresql
    encoding: unicode
    host: 0.0.0.0
    port: 15432
    # For details on connection pooling, see Rails configuration guide
    # https://guides.rubyonrails.org/configuring.html#database-pooling
    pool: &lt;%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %&gt;
    username: postgres
    password: Postgres2019!
  development:
    &lt;&lt;: *default
    database: mos_development
  </code></pre>
  
  <p>But whenever I try to run my application, Rails can't connect to the database returning </p>
  
  <pre><code>ActiveRecord::ConnectionTimeoutError
  </code></pre>
  
  <p>but  when I check my containers everything is running fine:</p>
  
  <pre><code>CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                     NAMES
  1cfbc010829a        project_web           "bundle exec puma -C…"   19 minutes ago      Up 13 minutes                                 project_web_1
  1774e2b89452        postgres              "docker-entrypoint.s…"   20 minutes ago      Up 13 minutes       0.0.0.0:15432-&gt;5432/tcp   project_db_1
  0ce7dbb6d735        project_nginx         "nginx -g 'daemon of…"   3 hours ago         Up 13 minutes       0.0.0.0:80-&gt;80/tcp        project_nginx_1
  </code></pre>
  
  <p>If my PostgreSQL container is running at <code>0.0.0.0:15432</code>, then my Rails container should be able to connect to it also at <code>0.0.0.0:15432</code>, right?</p>