Docker: Docker-compose ruby + postgres - PG::ConnectionBadcould not connect to server

Viewed 3856

I created Dockerfile to ruby from official docker-compose website. I used docker compose to integrate this with Postgres. When docker-compose up, then PostgreSQL starts but I cannot connect to this. The main problem is in ruby database config because I can't change to listen to another host and set the default user. I don't know how to connect these two containers without changing the ruby database config.

Dockerfile

FROM ruby:2.3.4
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

Docker-compose

version: '3'
services:
  db:
    image: postgres:9.6
    ports:
      - "5432:5432"
  web:
    build: .
    ports:
      - "4000:4000"
    depends_on:
      - db

Ruby database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: project_development

If i run inside web conatiner "bundle exec rake db:create I get error:

psql: 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 know what env set that will connect together.

3 Answers

Just to add to Bartosz Bonisławski's answer.

I encountered this issue when building a Rails application on Ubuntu 18.04.

My Dockerfile for my development environment which was located in docker/development/Dockerfile directory of my project was defined like this:

version: '3'

services:
  app:
    build:
      context: .
      dockerfile: ./docker/${RAILS_ENV}/Dockerfile
    depends_on:
      - database
      - redis
    ports:
      - "3000:3000"
    restart: always
    volumes:
      - .:/app
      - gem-cache:/usr/local/bundle/gems
      - node-modules:/app/node_modules
    env_file:
      - .env
    environment:
      RAILS_ENV: ${RAILS_ENV}
      RACK_ENV: ${RACK_ENV}

  database:
    image: postgres:12.1
    expose:
      - "5432"
    restart: always
    env_file:
      - .env
    environment:
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
      POSTGRES_DB: ${DATABASE_NAME}
      POSTGRES_HOST_AUTH_METHOD: ${DATABASE_HOST}

    volumes:
      - postgres-data:/var/lib/postgresql/data
volumes:
  gem-cache:
    driver: local
  node-modules:
    driver: local
  postgres-data:
    driver: local

While my .env file was defined as this:

DATABASE_USER=myapp_user
DATABASE_PASSWORD=myapp_password
DATABASE_NAME=myapp_development
DATABASE_HOST=database
DATABASE_PORT=5432

RAILS_ENV=development
RACK_ENV=development

The issue I had was that I did not add host config to my database.yml file of my project. So I was encountering the error:

psql: 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"?

Here's how I solved:

I simply added it like this and it worked perfectly fine:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: <%= ENV['DATABASE_NAME'] %>
  username: <%= ENV['DATABASE_USER'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  host: <%= ENV['DATABASE_HOST'] %>
  port: <%= ENV['DATABASE_PORT'] %>

That's all.

I hope this helps

Related