Docker Postgres Ruby on Rails无法连接

时间:2018-06-24 16:55:08

标签: ruby-on-rails ruby postgresql docker

我正在从docker Docker Rails开始学习本教程,并创建了一个文件夹,并将以下代码添加到了我的docker文件中。

 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

我的docker撰写文件代码为:

 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

我在运行docker时正在关注本教程,我只会看到此错误:

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

这是怎么回事,我不知道如何检查和检测错误如何解决。

2 个答案:

答案 0 :(得分:2)

您需要在web容器中使用环境变量,以便它知道如何连接到db容器。

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

答案 1 :(得分:0)

请转到您的SELECT p.name, m.title FROM Credits c JOIN People p ON p.id = c.person_id JOIN Movies m ON m.id = c.movie_id WHERE m.id IN ( SELECT movie_id FROM Credits c1 JOIN People p1 on p1.id = c1.person_id WHERE p1.name = 'Ben Affleck' ) ,并将主机集添加到database.yml ,然后添加用户名和密码,然后再次运行命令。