[英]Docker Compose: Rails won't respect the DATABASE_URL to connect to Postgres
我正試圖將我的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
如果我運行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"
但是如果我運行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!
知道我在這里做錯了嗎?
編輯:這是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
我有同樣的問題,似乎配置合並在Rails 5.1.x上以某種方式被破壞了
您可以通過在default
塊中添加url: <%= ENV['DATABASE_URL'] %>
來強制使用DATABASE_URL
。
我也有類似的問題,其原因是多方面的。
TL; DR,請確保:
app
容器已正確鏈接到您的postgres
容器並依賴於它 DATABASE_URL
引用了postgres
容器的正確主機名 RAILS_ENV
ENV變量在app
容器中顯式設置 首先,我必須確保我的docker_compose.yml
中的app
容器鏈接到postgres
容器並依賴於postgres
容器:
version: '3'
services:
app:
container_name: 'app'
depends_on:
- postgres
links:
- postgres
第二,我提供的DATABASE_URL
ENV變量database.yml
沒有引用postgres容器的正確主機名。 最初,我看起來像:
'postgres://postgres:@localhost:5432/my_app_development'
我改成了:
'postgres://postgres:@postgres:5432/my_app_development'
在你的情況下不是一個明顯的問題,但值得注意的是其他人。
第三,我沒有明確指定RAILS_ENV
環境。 我將ENV變量設置為docker_compose.yml
development
,以便使用正確的database.yml
配置。
我的database.yml
文件的最終結果:
development: &default
adapter: postgresql
encoding: unicode
database: my_app_development
username: postgres
pool: 5
timeout: 5000
url: <%= ENV['DATABASE_URL'] %>
test:
<<: *default
database: my_app_test
pool: 5
url: <%= ENV['DATABASE_URL'] %>
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
我的docker_compose.yml
文件的最終結果:
version: '3'
services:
app:
container_name: 'app'
environment:
DATABASE_URL: 'postgres://postgres:@postgres:5432/my_app_development'
RAILS_ENV: 'development'
build: .
ports:
- '3000:3000'
volumes:
- ./:/dev
depends_on:
- postgres
links:
- postgres
tty: true
postgres:
container_name: 'postgres'
image: postgres:9.5
ports:
- '5432:5432'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD:
之后,所有bundle exec rake db:
命令都運行得很好。
希望有所幫助!
問題出在rake任務中,它正在嘗試創建測試數據庫,我假設你的database.yml
有一個帶有localhost
主機的測試環境。
從rake db:create
的描述
從DATABASE_URL或config / database.yml為當前RAILS_ENV創建數據庫(使用db:create:all在配置中創建所有數據庫)。 沒有RAILS_ENV,它默認創建開發和測試數據庫
您應該明確地傳遞env,否則它可以默認創建一個測試數據庫。
我建議你改變測試數據庫credentionals在database.yml
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.