Ruby on rails 无法在Rails和Postgres Docker容器之间建立连接

Ruby on rails 无法在Rails和Postgres Docker容器之间建立连接,ruby-on-rails,postgresql,docker,docker-compose,Ruby On Rails,Postgresql,Docker,Docker Compose,我是Docker新手,尝试使用Docker compose将Rails容器和Postgres容器Docker化,并同时构建和启动,但当我尝试转到localhost:3009查看“耶,你在Rails上”页面时,Rails日志中出现以下错误: Started GET "/" for 172.27.0.1 at 2020-10-05 12:40:18 +0000 backend_1 | Cannot render console from 172.27.0.1! Allowed

我是Docker新手,尝试使用Docker compose将Rails容器和Postgres容器Docker化,并同时构建和启动,但当我尝试转到localhost:3009查看“耶,你在Rails上”页面时,Rails日志中出现以下错误:

Started GET "/" for 172.27.0.1 at 2020-10-05 12:40:18 +0000
backend_1   | Cannot render console from 172.27.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
backend_1   |   
backend_1   | PG::ConnectionBad (could not connect to server: Connection refused
backend_1   |   Is the server running on host "postgres" (172.27.0.2) and accepting
backend_1   |   TCP/IP connections on port 5439?
backend_1   | ):
这是我的docker-compose.yml:

version: '3'

volumes:
  db_data:
    driver: local
  app_data:
    driver: local

services:
  # database container
  postgres:
    image: postgres
    volumes:
      - app_data:/var/lib/postgresql/data
    ports:
      - 5439:5432
    environment:
      POSTGRES_DB: db_name
      POSTGRES_USER: user
      POSTGRES_PASSWORD: xxxxxx
    command: ["postgres", "-c", "log_statement=all"]
    restart: always

  app:
    build: ./server
    volumes:
      - ./server:/code
    ports:
      - "3009:3000"
    depends_on:
      - postgres
    environment:
      RAILS_ENV: development
      DATABASE_HOST: postgres
      DATABASE_PORT: 5439
      DATABASE_NAME: db_name
      DATABASE_USERNAME: user
      DATABASE_PASSWORD: xxxxxx
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
这是Dockerfile:

FROM ruby:2.7.1

WORKDIR /app

COPY . /app

# Install NodeJS and Yarn.
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn

RUN yarn install --check-files

RUN bundle install

CMD ["rails", "server", "-b", "0.0.0.0"]
这是我的数据库.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: <%= ENV['DATABASE_HOST'] %>
  port: <%= ENV['DATABASE_PORT'] %>
  database: <%= ENV['DATABASE_NAME'] %>
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>

development:
  <<: *default
  
test:
  <<: *default
default:&default
适配器:postgresql
编码:unicode
游泳池:
主持人:
端口:
数据库:
用户名:
密码:
发展:

正如戴维·梅兹指出的那样:

需要将数据库_端口设置为普通PostgreSQL端口5432;端口:对于集装箱间通信,端口被忽略(且不必要)


正如戴维·梅兹指出的那样:

需要将数据库_端口设置为普通PostgreSQL端口5432;端口:对于集装箱间通信,端口被忽略(且不必要)


您需要将
数据库_端口
设置为普通PostgreSQL端口5432<代码>端口:
对于集装箱间的通信是被忽略的(并且是不必要的)。谢谢,这是我不知道的一条重要信息。您需要将
数据库\u端口
设置为普通的PostgreSQL端口5432<代码>端口:对于集装箱之间的通信被忽略(而且是不必要的)。谢谢,这是我不知道的一条重要信息