使用docker-compose在执行第一个命令后,执行`rake db:setup`的docker容器中的Rails 5始终会断开与postgresql的连接

时间:2019-01-29 13:37:49

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

我有一个Rails 5 docker-compose项目(Github link),可以在多个容器中正常工作。

我可以使用docker-compose exec app rails db:create创建数据库,并在localhost:3000上运行项目

问题在于它总是错误: could not connect to server: No such file or directory成功完成命令后。

当我要使用执行其他3个命令的db:setup时,这是一个问题。它先完成第一个命令,然后执行错误,提示连接断开,无法执行其余2个命令。

是否关闭了命令之间的连接?我该如何进一步调试呢?

Rails 5.2.1,第1.1.3页Gemfile

RAILS_ENV.env文件中定义。在这种情况下是:

root@3ec00f6534aa:/app# printenv|grep RAILS
RAILS_ENV=development

这是执行docker-compose exec app bash后从应用程序容器内输出的错误。 注意第二行,说数据库已创建:

root@b281e881b96a:/app# rake db:setup
Created database 'sc_dev'
could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>10, "database"=>"smartcitizen_testing"}
rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
/usr/local/bundle/gems/pg-1.1.3/lib/pg.rb:56:in `initialize'
/usr/local/bundle/gems/pg-1.1.3/lib/pg.rb:56:in `new'
/usr/local/bundle/gems/pg-1.1.3/lib/pg.rb:56:in `connect'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:684:in `connect'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:215:in `initialize'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:40:in `new'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:40:in `postgresql_connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:809:in `new_connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:832:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:793:in `acquire_connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:521:in `checkout'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:380:in `connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1008:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/connection_handling.rb:90:in `connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/postgresql_database_tasks.rb:12:in `connection'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/postgresql_database_tasks.rb:21:in `create'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:119:in `create'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:313:in `each'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:138:in `create_current'
/usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <main>'
/usr/local/bundle/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:setup => db:schema:load_if_ruby => db:create
(See full trace by running task with --trace)

1 个答案:

答案 0 :(得分:1)

您正在运行rails db:create,但未指定环境。因此,Rails会努力为您节省一些精力,并在开发和测试环境中都运行该命令(我不记得它们以什么顺序运行)。您会看到sc_dev数据库已创建,但是错误提示它无法为smartcitizen_testing数据库创建数据库。

Rails可以连接到您的开发数据库,​​但不能连接到测试数据库。

经过反复评论,我了解到您正在使用DATABASE_URL环境变量,这是有问题的,因为测试数据库和开发数据库实际上并不共享相同的数据库URL。解决方案是修改database.yml,以便可以通过环境变量独立配置开发和测试数据库,从而允许它们在运行单个rails db:create命令时各自具有自己的设置。