Busch
Busch

Reputation: 959

Why are rails connections not working in docker-compose?

I setup a very basic rails app that connects to a postgres instance and a rails instance. For some reason, when I execute rails db:setup, it won't connect to the database. My setup is below. Don't think it can get much simpler.

Important to note, postgres was up and ready to accept connections.

docker-compose.yml

version: '3.4'

services:
  postgres:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=password
    volumes:
      - ./tmp/postgres/data:/var/lib/postgres/data
  redis:
    image: redis
    ports:
      - '6379:6379'
  web:
    build:
      context: .
      target: development
    command: tail -f /dev/null # I exec'ed in to run command
    depends_on:
      - postgres
      - redis
    env_file:
      - ./docker-dev.env
    ports:
      - '3000:3000'

database.yml


development:
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres
  password: password
  host: postgres

The error ouput

D, [2020-04-03T03:20:34.562188 #32] DEBUG -- : using default configuration
could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
Couldn't create 'hinge_test_db' database. Please check your configuration.
rails aborted!
PG::ConnectionBad: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?

Upvotes: 0

Views: 405

Answers (1)

nishanth
nishanth

Reputation: 95

The error is not related to docker but related to postgres, open postgres configuration file pg_hba.conf and set it to allow permissions from localhost and restart the postgres server. Hope it helps 👍

Upvotes: 1

Related Questions