I'm trying to containerize my Ruby on Rails 5.1.0 application, but I'm having some trouble with it not picking up DATABASE_URL from the environment. In docker-compose.yml, I have the following service:
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
The environment gets picked up just fine if I run 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"
But then if I run docker-compose run app rake db:create, I get an error about not being able to connect to 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!
Any idea what I'm doing wrong here?
Edit: Here's what database.yml looks like:
common: &common
adapter: postgresql
pool: 10
timeout: 5000
development:
<<: *common
database: myapp_development
test:
<<: *common
database: myapp_test
production:
<<: *common
database: myapp_production