前提・実現したいこと
DockerでRuby on Railsの環境構築をしたいです。
以下のコマンドを打つと以下のようなエラーが出て、データベースに接続されないようです。解決策をお教え下さい。
発生している問題・エラーメッセージ
$ docker-compose run web rake db:create Starting rails-on-docker_db_1 ... done could not translate host name "db" to address: Name or service not known Couldn't create 'myapp_development' database. Please check your configuration. rake aborted! PG::ConnectionBad: could not translate host name "db" to address: Name or service not known /usr/local/bundle/gems/pg-1.1.4/lib/pg.rb:56:in `initialize' /usr/local/bundle/gems/pg-1.1.4/lib/pg.rb:56:in `new' /usr/local/bundle/gems/pg-1.1.4/lib/pg.rb:56:in `connect' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:692:in `connect' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:223:in `initialize' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `new' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `postgresql_connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:855:in `checkout_new_connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:834:in `try_to_checkout_new_connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:795:in `acquire_connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:523:in `checkout' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1014:in `retrieve_connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/postgresql_database_tasks.rb:12:in `connection' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/postgresql_database_tasks.rb:21:in `create' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:119:in `create' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:313:in `each' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:138:in `create_current' /usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>' /usr/local/bundle/gems/rake-12.3.2/exe/rake:27:in `<top (required)>' Tasks: TOP => db:create (See full trace by running task with --trace)
該当のソースコード
config/database.yml
1 2 3default: &default 4 adapter: postgresql 5 encoding: unicode 6 database: myapp_developmen 7 host: db 8 username: postgres 9 password: 10 pool: 5 11 12development: 13 <<: *default 14 database: myapp_development 15 16test: 17 <<: *default 18 database: myapp_test 19 20# <<: *default 21# database: myapp_production 22# username: myapp 23# password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
docker
1version: '3' 2services: 3 db: 4 image: postgres:10 5 volumes: 6 - ./tmp/db:/var/lib/postgresql/data 7 web: 8 build: . 9 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 10 volumes: 11 - .:/myapp 12 ports: 13 - "3000:3000" 14 depends_on: 15 - db
試したこと
参考:公式リファレンス
1.
/tmp/dbの中身を空にした。
2.
docker-compose.ymlで
image:postgres:10に変更した。
他は参考と同じ。
3.
docker-compose run web rails db:create
でも実行したが、変化はなかった。
補足情報(FW/ツールのバージョンなど)
postgresqlで動かしたいです。

