前提・実現したいこと
Docker Composeを使用してRails6+PostgreSQLの環境構築をしていて、先ずはRailsサーバーを立ち上げたいのですが、PostgreSQLに上手く接続できません。
下記、該当のソースコードに記載したファイルを作成後に
docker-compose run web rails new . --force --database=postgresqlを実行後、
Gemfileの# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
の部分をgem 'pg', '>= 0.20.0', '< 2.0'に修正し、docker-compose buildを実行しdocker compose run web db:createをした際に下記エラーメッセージが出力されます。
お手数ですが、解決策をご教授ください。
発生している問題・エラーメッセージ
エラーメッセージ $ docker-compose run web rake db:create Starting rails_db_1 ... done /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/type.rb:27: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/type/adapter_specific_registry.rb:9: warning: The called method `add_modifier' is defined here 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.2.2/lib/pg.rb:58:in `initialize' /usr/local/bundle/gems/pg-1.2.2/lib/pg.rb:58:in `new' /usr/local/bundle/gems/pg-1.2.2/lib/pg.rb:58:in `connect' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:46:in `postgresql_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:889:in `new_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:933:in `checkout_new_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:912:in `try_to_checkout_new_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:873:in `acquire_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:595:in `checkout' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:439:in `connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1121:in `retrieve_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:238:in `retrieve_connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:206:in `connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/postgresql_database_tasks.rb:12:in `connection' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/postgresql_database_tasks.rb:21:in `create' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:126:in `create' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:185:in `block in create_current' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:476:in `each' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:475:in `each' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:184:in `create_current' /usr/local/bundle/gems/activerecord-6.0.2.1/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <main>' Tasks: TOP => db:create (See full trace by running task with --trace) ### 該当のソースコード(Gemfile.lockも作成しましたが、特に何も記述せずに作成したので、省略します。) Dockerfile ---------------------------------------- FROM ruby:2.7.0 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev node.js RUN mkdir /myapp WORKDIR /myapp COPY Gemfile /myapp/Gemfile COPY Gemfile.lock /myapp/Gemfile.lock RUN bundle install COPY . /myapp ---------------------------------------- docker-compose.yml ---------------------------------------- version: '3' services: db: image: postgres web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/myapp ports: - "3000:3000" depends_on: - db ---------------------------------------- Gemfile ---------------------------------------- source 'https://rubygems.org' gem 'rails', '6.0.0.0' ---------------------------------------- ### 試したこと ログに出力されているPG::ConnectionBad: could not translate host name "db" to address: Name or service not knownの箇所を検索して、https://teratail.com/questions/195511に書かれている事など実行したのですが、上手く行きませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/28 11:49