【发布时间】:2020-06-05 17:11:15
【问题描述】:
我正在尝试使用 docker-compose 来运行 Rails/Postgres 应用程序,但在不设置 POSTGRES_HOST_AUTH_METHOD="trust" 的情况下创建数据库时遇到问题。以下 docker-compose.yml 似乎与“信任”一起工作得很好:
version: '3'
services:
db:
image: postgres:alpine
env_file: .env
environment:
POSTGRES_HOST_AUTH_METHOD: "trust"
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build:
context: .
dockerfile: Dockerfile
command: bundle exec rails server -b '0.0.0.0' -p 3000
env_file: .env
ports:
- "3000:3000"
volumes:
- .:/app
- node_modules:/app/node_modules/
depends_on:
- db
volumes:
node_modules:
为了在没有“信任”的情况下进行这项工作,我知道在初始化 Postgres 图像以设置默认用户密码时,我必须定义环境变量 POSTGRES_PASSWORD。因此,我们使用 docker-compose 已经引用的以下 .env 文件:
POSTGRES_URL=db
POSTGRES_PASSWORD=foobar
并更改 config/database.yml 以使用以下默认连接配置:
...
default: &default
adapter: postgresql
encoding: unicode
host: <%= ENV['POSTGRES_URL'] %>
# If POSTGRES_PASSWORD is set in the environment then use the default user
# and that password to connect. If none is set, then assume that the creds
# are elsewhere (e.g. included in POSTGRES_URL on Heroku)
<% if ENV['POSTGRES_PASSWORD'].present? %>
username: postgres
password: <%= ENV['POSTGRES_PASSWORD'] %>
<% end %>
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
...
然后我在控制台中执行以下操作:
$ docker-compose build
db uses an image, skipping
Building web
...
$ docker-compose run web echo container started successfully
Starting rails-dockerized_db_1 ... done
container started successfully
$ docker-compose run web bundle exec rails db:create
Starting rails-dockerized_db_1 ... done
FATAL: password authentication failed for user "postgres"
Couldn't create 'rails_dockerized_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: FATAL: password authentication failed for user "postgres"
...
据我所知,这个配置应该是正确的,但是我显然忽略了一些东西。为什么我看到此身份验证失败?
【问题讨论】:
标签: ruby-on-rails postgresql docker docker-compose