4

I am following this tutorial from docker Docker Rails and I have created a folder and added this code below in my docker file.

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

And my docker compose file code is:

 version: '3'
services:
  db:
    image: postgres
    volumes:
      - .data:/var/lib/postgresql/data
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

I am following tutorial when I am running docker compose up I can just see this error:

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

What is wrong here I don't know how to inspect and detect error how to fix this.

2 Answers 2

7

You need environment variables within your web container so that it knows how to connect to the db container.

version: '3'
services:
  db:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=
    volumes:
      - ./data:/var/lib/postgresql/data
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    environment:
      - PGHOST=db
      - PGUSER=postgres
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
Sign up to request clarification or add additional context in comments.

1 Comment

when I changed - .data:/var/lib/postgresql/data to - ./data:/var/lib/postgresql/data it solved my problem. Thanks for answering
0

Please go to your database.yml and add host set to db , then username and password and run the command again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.