2

Im trying to connect to a Postgres database within a Ruby/Docker container with;

docker-compose run app rails db:setup

only my stack trace reports;-

Starting reptrax-uk_postgres_1 ... done
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 database for {"host"=>"localhost", "port"=>5432, "adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"development", "pool"=>10, "username"=>"user", "password"=>"secret"}
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?

So I issue a

netstat -antu | grep 5432

to check for anything relating to the offending port but nothing, it returns blank.

Here's a snapshot of my docker-compose.yml

version: '3'

services:
  app:
    build: .
    command: sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    environment:
      - RABBITMQ_HOST=rabbitmq
      - MONGO_HOST=mongo
      - POSTGRES_HOST=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASS=
    volumes:
      - .:/app
      - bundle:/bundle
      - node_modules:/app/node_modules
    ports:
      - "3000:3000"
    depends_on:
      - postgres
      - mongo
      - rabbitmq
      - redis
      - chrome
  postgres:
    image: postgres:9.5
    ports:
      - "5432:5432"
  mongo:
    image: mongo
    ports:

I tried a whole manner of docker commands yesterday including;-

docker-compose stop
docker-compose down
docker system prune
docker pull postgres:9.5

but still no joy.

Are you able to troubleshoot?

cookie
  • 222
  • 3
  • 8
  • 18

1 Answers1

0

worked for me;-

In your docker-compose.yml under the postgres setting add the following entry;-

postgres:
    image: postgres:9.5
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
    ports:
      - "5432:5432"

then at the terminal - continue with;-

docker-compose down
docker container stop $(docker container ls -aq)
docker network create x_middleware
docker network create foo
docker-compose build
docker-compose run app bin/rails db:setup profiles:build
docker-compose run app bin/rails profiles:build
docker-sync-stack start
cookie
  • 222
  • 3
  • 8
  • 18