我有一个新的Rails应用程序,我打算在Docker容器中部署。我们使用structure.sql
代替schema.rb
。在我的本地设置中(使用docker-compose文件,Postgres在一个单独的容器中),当我运行rails db:setup
时,我收到以下错误:
rails aborted!
failed to execute:
psql -q -f /rails/db/structure.sql cappy_dev
Please check the output above for any errors and make sure that `psql` is installed in your PATH and has proper permissions.
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/postgresql_database_tasks.rb:99:in `run_cmd'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/postgresql_database_tasks.rb:71:in `structure_load'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:213:in `structure_load'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:226:in `load_schema'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:253:in `block in load_schema_current'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:292:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:291:in `each'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:291:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/tasks/database_tasks.rb:252:in `load_schema_current'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/railties/databases.rake:306:in `block (3 levels) in <top (required)>'
/usr/local/bundle/gems/activerecord-5.0.2/lib/active_record/railties/databases.rake:310:in `block (3 levels) in <top (required)>'
/usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/usr/local/bundle/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/usr/local/bundle/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'
bin/rails:9:in `require'
bin/rails:9:in `<main>'
Tasks: TOP => db:structure:load
(See full trace by running task with --trace)
这是有道理的,因为我的Rails容器只有与Ruby和Rails相关的工具,并且没有Postgres容器中的Postgres或pqsl。
如果我的Rails容器中没有psql,pg_dump和其他Postgres工具,如何运行rails db:setup
(或db:migrate
或其他任务)?
更新:这是我的docker-compose文件:
version: '3'
services:
postgres:
image: postgres:9.6
volumes:
- cappy-data:/var/lib/postgresql/data
ports:
- '5432:5432'
environment:
POSTGRES_PASSWORD: postgres
cappy:
image: REDACTED.dkr.ecr.us-east-1.amazonaws.com/cappy:latest
build:
context: .
command: rails s -p 3000 -b '0.0.0.0'
depends_on:
- postgres
volumes:
- .:/rails
ports:
- "3000:3000"
links:
- "postgres:postgres"
environment:
RAILS_ENV: development
volumes:
cappy-data:
我不认为这个文件是问题 - db:setup
可以创建数据库,它只是无法加载structure.sql
因为它似乎无法找到psql
}。
答案 0 :(得分:0)
我尝试了几件事 - 运行rails db:create
然后db:migrate
会设置本地数据库,但从长远来看它不会起作用,因为当你运行时{ {1}},在任务结束时转储模式。使用Postgres,并且rails db:migrate
设置为schema_format
(这是生成:sql
文件的内容),rake任务依赖于Postgres工具,如psql和pg_dump。
最后,处理它的最佳方法是将postgresql-client(不需要整个服务器)添加到Rails Docker镜像。它拥有手头所需的工具,一切正常。