mardi 30 mai 2017

Unable to connect to database from rails console on dockers

I have my rails project setup on Digitalocean using Dockers. This is my docker-compose.prod.yml file.

version: "2"

volumes:
 db-data:
   external: false

services:
 db:
  image: postgres
  env_file: .env.production
 volumes:
   - db-data:/var/lib/postgresql/data

app:
 build: .
 env_file: .env
 environment:
   RAILS_ENV: production
 ports:
   - "3000:3000"
 depends_on:
   - db

and this is my database.yml file

default: &default
 adapter: postgresql
 encoding: unicode
 host: db
 username: <%= ENV["POSTGRES_USER"] %>
 password: <%= ENV["POSTGRES_PASSWORD"] %>
 # For details on connection pooling, see rails configuration guide
 # http://ift.tt/1k2jGKr
 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
 <<: *default
 database:  project_development

test:
 <<: *default
 database: project_test

production:
 <<: *default
 host: <%= ENV["POSTGRES_HOST"] %>
 database: project_production

I have created two docker images. One for app and one for postgres/db.

My application is working fine on production. I am able to create and delete records from my web page on production.

But, when I try to access rails console of production database from docker bash by following commands:

docker run -i -t project_app:latest bash 

to access console:

RAILS_ENV=production rails c

Inside Rails Console whenever I try to access any model data or try to perform any query(i.e Model.first etc) I am unable to access postgres database. It shows me following error:

PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

I have tried many possible solutions to resolve this issue but unable to resolve this.

Please help me! Thanks in advance

Aucun commentaire:

Enregistrer un commentaire