我有一个Rails5,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
时,这是一个问题。它完成第一个命令,然后出现错误,表示连接断开,无法执行剩下的两个命令。
是否有什么东西关闭了命令之间的连接?我如何进一步调试它呢?
Rails 5.2.1,pg 1.1.3 Gemfile
在.env
文件中定义RAILS_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)
发布于 2019-01-29 13:48:58
您正在运行rails db:create
,但未指定环境。因此,Rails试图为您节省一些精力,并同时为开发和测试环境运行该命令(我记不清它们的运行顺序了)。您看到创建了sc_dev
db,但是错误声明它无法为smartcitizen_testing
数据库创建数据库。
Rails可以连接到开发数据库,但不能连接到测试数据库。
经过一些来回的评论,我了解到您使用的是DATABASE_URL
环境变量,这是有问题的,因为测试和开发数据库实际上并不共享相同的数据库url。解决方案是修改database.yml
,以便可以通过环境变量独立配置开发和测试数据库,从而允许在运行单个rails db:create
命令时每个数据库都有自己的设置。
https://stackoverflow.com/questions/54422361
复制