質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

3回答

6707閲覧

docker-composeでのRuby on Rails環境構築でPostgreSQLが接続されない。

sawady0920

総合スコア13

docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2019/06/17 16:04

編集2019/06/17 21:41

前提・実現したいこと

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で動かしたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/06/17 20:35

`docker-compose.yml` を書いてください。
guest

回答3

0

投稿2019/06/20 15:20

編集2019/06/20 15:21
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

調べてみると次のようなやり方を紹介しているものが多かったです。
【ゆるふわ????】Docker + Rails + Heroku =「Yay! You’re on Rails!」【本番環境構築】

僕も最近docker+railsでherokuにデプロイを試しています。
上記の方法とは違うのであまりよくない方法かもしれませんが一応紹介します。
<database.yml>

host: <%= ENV.fetch('DATABASE_HOST') %>

heroku側で環境変数(DATABASE_HOST)を設定する
ダッシュボード -> 該当アプリ -> settings -> Reveal config vars から設定
databaseのアドレスは次のコマンドで確認する。
heroku pg:credentials:url --app <herokuアプリ名>

投稿2019/06/20 15:05

編集2019/06/20 15:12
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

config/database.ymlのuserをrootにしてみるとどうでしょうか?

投稿2019/06/18 00:37

t_kusakabe74

総合スコア549

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sawady0920

2019/06/18 15:36

$ cat config/database.yml # PostgreSQL. Versions 9.1 and up are supported. # # Install the pg driver: # gem install pg # On OS X with Homebrew: # gem install pg -- --with-pg-config=/usr/local/bin/pg_config # On OS X with MacPorts: # gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config # On Windows: # gem install pg # Choose the win32 build. # Install PostgreSQL and put its /bin directory on your path. # # Configure Using Gemfile # gem 'pg' # default: &default adapter: postgresql encoding: unicode database: myapp_development # For details on connection pooling, see Rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling host: db username: root password: pool: 5 development: <<: *default database: myapp_development # The specified database role being used to connect to postgres. # To create additional roles in postgres see `$ createuser --help`. # When left blank, postgres will use the default role. This is # the same name as the operating system user that initialized the database. #username: myapp # The password associated with the postgres role (username). #password: # Connect on a TCP socket. Omitted by default since the client uses a # domain socket that doesn't need configuration. Windows does not have # domain sockets, so uncomment these lines. #host: localhost # The TCP port the server listens on. Defaults to 5432. # If your server runs on a different port number, change accordingly. #port: 5432 # Schema search path. The server defaults to $user,public #schema_search_path: myapp,sharedapp,public # Minimum log levels, in increasing order: # debug5, debug4, debug3, debug2, debug1, # log, notice, warning, error, fatal, and panic # Defaults to warning. #min_messages: notice # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: myapp_test # As with config/secrets.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is # ever seen by anyone, they now have access to your database. # # Instead, provide the password as a unix environment variable when you boot # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database # for a full rundown on how to provide these environment variables in a # production deployment. # # On Heroku and other platform providers, you may have a full connection URL # available as an environment variable. For example: # # DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" # # You can use this database configuration with: # # production: # url: <%= ENV['DATABASE_URL'] %> # #production: # <<: *default # database: myapp_production # username: myapp # password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %> docker-compose.ymlを以上の内容に変更いたしました。 以下のコマンドを改めて実行したところ、実行結果に変化はないように思われます。 $ docker-compose run web rails 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. rails 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/railties-5.2.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands/rake/rake_command.rb:20:in `perform' /usr/local/bundle/gems/railties-5.2.3/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-5.2.3/lib/rails/commands.rb:18:in `<top (required)>' /usr/local/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require' /usr/local/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require' /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency' /usr/local/bundle/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require' /myapp/bin/rails:9:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run' /usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load' /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>' /myapp/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:create (See full trace by running task with --trace)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問