简体   繁体   中英

Docker Postgres Ruby on Rails unable to connect

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.

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

请转到您的database.yml并将主机集添加到db ,然后添加用户名和密码并再次运行该命令。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM