You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 7.1.0+Docker环境下database.yml YAML语法错误求助

问题

我是Ruby on Rails新手,在Pop!_OS 22.04 LTS电脑上,用Ruby 3.2.2、PostgreSQL 16.0,基于Docker搭建Rails 7.1.0应用。docker-compose build编译正常,但运行docker-compose up时提示bin/rails aborted,报错信息如下:

sharkphonesuniverse-demo-web-1  | YAML syntax error occurred while parsing
/rails/config/database.yml. Please note that YAML must be consistently
indented using spaces. Tabs are not allowed. Error: (<unknown>): found
character that cannot start any token while scanning for the next token at
line 14 column 13

我希望通过Docker启动服务器,在浏览器访问localhost:3000,但未能成功。已经确保database.yml使用空格统一缩进,且经yamllint验证有效,但仍出现相同错误。

我的database.yml内容:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: <%= ENV['POSTGRES_HOST'] %>
  port: 5432

development:
  <<: *default
  database: sharkphonessale_development
  username: <%= ENV['POSTGRES_USER'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>

test:
  <<: *default
  database: sharkphonessale_test
  username: <%= ENV['POSTGRES_USER'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>

production:
  <<: *default
  database: <%= ENV['POSTGRES_DB'] %>
  username: <%= ENV['POSTGRES_USER'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>

.env文件内容:

RAILS_ENV=production
POSTGRES_HOST=db
POSTGRES_DB=demodb_production
POSTGRES_USER=my db username goes here
POSTGRES_PASSWORD=my db password goes here
RAILS_MASTER_KEY=I paste the content of my config > master.key file here
解决方法
  • 修改.env文件,给包含空格的环境变量值添加双引号:
    POSTGRES_USER="my db username goes here"
    POSTGRES_PASSWORD="my db password goes here"
    
  • 重启Docker容器:docker-compose down && docker-compose up

原因:YAML语法中,包含空格或特殊字符的字符串必须用引号包裹。你的.env里的用户名和密码带有空格,未加引号的情况下,Rails通过ERB渲染database.yml时,会生成不带引号的字符串,导致最终的YAML内容不符合语法规范,触发解析错误。

内容的提问来源于stack exchange,提问作者Jephry_x

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 05:13:15