Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, Postgres and Docker having PG::ConnectionBad "/var/run/postgresql/.s.PGSQL.5432"?

I'm developing with containers and using docker-compose to lift them up. From time to time I'll run docker-compose down and will rebuild the containers. Most of the times I get this 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 don't get why Rails is complaining about Postgres not being there, if the entire containers are being rebuilt again. Also, the error just disappears after a while, without me doing anything other stopping, re-upping or rebuilding the containers. I moved over to developing with containers to precisely escape from annoying issues like this.

This are the contents of my docker-compose.yml

version: '2'
services:
  users:
    build:
      context: '.'
    links:
      - postgres
      - redis
    volumes:
     - ".:/app/users/"
    working_dir: /app/users
    command: [rails, server, -p, "3000", -b, 0.0.0.0]
    ports:
    - "3000:3000"

  postgres:
    image: postgres:9.5
    environment:
      POSTGRES_DB: somedb
      POSTGRES_USER: someuser
      POSTGRES_PASSWORD: somepass

  redis:
    image: redis:latest
    command: redis-server
    ports:
      - "6379:6379"
    volumes:
      - "redis:/var/lib/redis/data"

volumes:
  redis:

database.yml

development:
  <<: *default
  database: somedb
  user: someuser
  password: somepass
  host: postgres
  port: 5432

Am I missing extra configurations on the postgres service or is it related to the users container?

like image 236
Sebastialonso Avatar asked Oct 29 '22 19:10

Sebastialonso


1 Answers

Add in docker-compose.yml users

 version: '2'
 services:
   users:
    environment:
        DATABASE_URL:postgres://user:pass@postgres:5432/datex_dev?pool=5&encoding=utf-8
   ....

Add in database.yml

 development:
   <<: *default
   url: <%= ENV['DATABASE_URL'] %>  
   ...

See Docker - PG::ConnectionBad

like image 165
Ruby232 Avatar answered Nov 15 '22 07:11

Ruby232