我想containerize我的Ruby on Rails的5.1.0應用程序,但我有一些麻煩,它沒有從環境拿起DATABASE_URL
。在docker-compose.yml
,我有以下服務:碼頭工人撰寫:Rails會不會尊重DATABASE_URL連接到Postgres的
app:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
environment:
DATABASE_URL: postgres://postgres:[email protected]: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:[email protected]: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
我可以與你分享我的解決方案,但它不直接回答你的問題,所以我把它置於評論中。你可以添加獨立的容器到postgres,添加主機配置到你的database.yml匹配postgres容器名稱。然後登錄容器,你將能夠運行任何你想要的命令,沒有錯誤。如果你有興趣,我可以提供詳細的答案。 –