4

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://guides.rubyonrails.org/configuring.html#database-pooling
 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

Rails Developer
  • 402
  • 6
  • 18

2 Answers2

1

After trying alot at last I found a solution. I was using docker run -i -t project_app:latest bash command to access container and to run rails console command which was wrong.

The correct command to access container's terminal is

 docker exec -it project_app /bin/bash

After accessing terminal I was able to successfully run query i.e User.first inside rails console.

Rails Developer
  • 402
  • 6
  • 18
0

Here: docker run -i -t project_app:latest bash you are not providing the env vars that your application need to use as credentials.

So use your docker-compose that is already setting env vars here:

env_file: .env

So run as this:

docker run --env-file .env -i -t project_app:latest bash
Robert
  • 33,429
  • 8
  • 90
  • 94
  • When, I use this command, I am getting following error: `PG::ConnectionBad: could not translate host name "db" to address: Name or service not known` – Rails Developer May 31 '17 at 16:40
  • It seems that you need to start the hole docker-compose stuff. It's simple: `docker-compose up -d` then `docker-compose exec app bash`. Is that what you need? – Robert May 31 '17 at 16:49
  • Docker compose is already started. Conatiners are already working. But in rails console of environment production. I am unable to access data. for example If I run any command like `User.first` it shows me error `PG::ConnectionBad: could not translate host name "db" to address: Name or service not known` – Rails Developer May 31 '17 at 16:55
  • If the containers are already running, just get into them, as this: `docker-compose exec app bash` or `docker exec -it container-id bash` – Robert Jun 01 '17 at 21:17
  • I can enter into `containers` and `rails c` but I am unable to query database. 'User.first' etc shows me error that I have mentioned above – Rails Developer Jun 02 '17 at 17:45