Sam*_*ing 6 postgresql ruby-on-rails docker docker-compose
我正在尝试对我的 Ruby on Rails 5.1.0 应用程序进行容器化,但我遇到了一些无法DATABASE_URL从环境中提取的问题。在docker-compose.yml,我有以下服务:
app:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
environment:
DATABASE_URL: postgres://postgres:pw@db:5432/myapp_development
Run Code Online (Sandbox Code Playgroud)
如果我运行,环境会被很好地恢复docker-compose run app rails c:
$ docker-compose run app rails c
Running via Spring preloader in process 25
Loading development environment (Rails 5.1.0)
irb(main):001:0> ENV['DATABASE_URL']
=> "postgres://postgres:pw@db:5432/myapp_development"
Run Code Online (Sandbox Code Playgroud)
但是如果我运行docker-compose run app rake db:create,我会收到一个关于无法连接到的错误localhost:
$ docker-compose run app rake db:create
Database 'myapp_development' already exists
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Couldn't create database for {"adapter"=>"postgresql", "host"=>"localhost", "pool"=>10, "timeout"=>5000, "database"=>"myapp_test"}
rake aborted!
Run Code Online (Sandbox Code Playgroud)
知道我在这里做错了什么吗?
编辑:这database.yml看起来像:
common: &common
adapter: postgresql
pool: 10
timeout: 5000
development:
<<: *common
database: myapp_development
test:
<<: *common
database: myapp_test
production:
<<: *common
database: myapp_production
Run Code Online (Sandbox Code Playgroud)
问题出在 rake 任务中,它正在尝试创建测试数据库,我假设您database.yml有一个带有主机的测试环境localhost。
从描述来看rake db:create
从 DATABASE_URL 或config/database.yml为当前 RAILS_ENV创建数据库(使用 db:create:all 创建配置中的所有数据库)。如果没有 RAILS_ENV,则默认创建开发和 测试数据库
您应该明显地传递环境,否则它可能会默认创建一个测试数据库。
我建议您更改database.yml.
| 归档时间: |
|
| 查看次数: |
4436 次 |
| 最近记录: |