fe_sendauth:未提供密码(PG :: ConnectionBad)Docker容器

时间:2020-02-21 18:03:50

标签: ruby-on-rails postgresql docker docker-compose

我遵循了一个教程,以对现有的Rails应用程序进行docker化。该应用程序在 development 环境中成功运行。但是,当我尝试以 stating env运行时,出现了错误。我保留了相同的配置进行说明和开发。这是我的相关文件:

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  pool: 10
  timeout: 5000
  database: <%= ENV['DATABASE_NAME'] %>
  username: <%= ENV['DATABASE_USER_NAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  host: db
  port: 5432

staging:
  <<: *default
  pool: 10
  timeout: 5000
  database: <%= ENV['DATABASE_NAME'] %>
  username: <%= ENV['DATABASE_USER_NAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  host: db
  port: 5432

application.yml

development:
  DATABASE_USER_NAME: 'developer'
  DATABASE_PASSWORD: 'qmwneb10PG'
  DATABASE_NAME: 'concern_box'
staging:
  DATABASE_USER_NAME: 'developer'
  DATABASE_PASSWORD: 'qmwneb10PG'
  DATABASE_NAME: 'concern_box'

docker-compose.yml

version: '3'
services:
  app:
    build: .
    container_name: cb_app_container
  db:
    container_name: cb_db_container
    image: postgres:9.6
    hostname: postgres
    ports:
      - "5432:5432"

docker-compose.stag.yml

version: '3'
services:
  app:
    environment:
      RAILS_ENV: staging
    command: bundle exec rails s -p 80 -b '0.0.0.0'
    ports:
      - "80:80"
    depends_on:
      - db

  db:
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

docker-compose.dev.yml

version: '3'
services:
  app:
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    stdin_open: true
    tty: true
    volumes:
      - .:/concern-box
    ports:
      - "3001:3000"
    depends_on:
      - db

  db:
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

init.sql

CREATE USER developer WITH PASSWORD 'qmwneb10PG';
CREATE DATABASE concern_box;
GRANT ALL PRIVILEGES ON DATABASE concern_box TO developer;

我正在使用以下命令来旋转容器docker-compose -f docker-compose.yml -f docker-compose.dev.yml up --force-recreate进行开发环境,并旋转docker-compose -f docker-compose.yml -f docker-compose.stag.yml up --force-recreate来作为临时环境运行。

1 个答案:

答案 0 :(得分:1)

在database.yml上,developmentstaging正在从ENV []加载,您的容器应该具有从Os环境加载的机密

别忘了将您的机密加载到主机环境中,或使用docker-compose中的env_file功能

docker-compose.stag.yml

version: '3'
services:
  app:
    environment:
      RAILS_ENV: staging
      DATABASE_USER_NAME: ${DATABASE_USER_NAME}  # <-- Add this
      DATABASE_PASSWORD: ${DATABASE_PASSWORD}    # <-- Add this
      DATABASE_NAME: ${DATABASE_NAME}            # <-- Add this
    command: bundle exec rails s -p 80 -b '0.0.0.0'
    ports:
      - "80:80"
    depends_on:
      - db

  db:
    expose:
      - 5432    # Just to make sure app will see the port
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

docker-compose.dev.yml

version: '3'
services:
  app:
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    environment:
      DATABASE_USER_NAME: ${DATABASE_USER_NAME}  # <-- Add this
      DATABASE_PASSWORD: ${DATABASE_PASSWORD}    # <-- Add this
      DATABASE_NAME: ${DATABASE_NAME}            # <-- Add this
    stdin_open: true
    tty: true
    volumes:
      - .:/concern-box
    ports:
      - "3001:3000"
    depends_on:
      - db

  db:
    expose:
      - 5432    # Just to make sure app will see the port
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

从ENV []而不是文件application.yml

中加载database.yml
database.yml
...
staging:
  <<: *default
  pool: 10
  timeout: 5000
  database: <%= ENV['DATABASE_NAME'] %>
  username: <%= ENV['DATABASE_USER_NAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  host: db
  port: 5432
...

application.yml
staging:
  DATABASE_USER_NAME: 'developer'
  DATABASE_PASSWORD: 'qmwneb10PG'
  DATABASE_NAME: 'concern_box'