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

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

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

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

解決済

1回答

9338閲覧

【docker】could not translate host name "db" to address: Name or service not known

dxo

総合スコア16

docker-compose

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

0クリップ

投稿2020/05/04 05:00

編集2020/05/04 07:45

$ docker-compose run --rm web rails db:create RAILS_ENV=development
を叩いた時に、以下のようなエラーが発生します。

エラーの内容

Starting hoge-master_db_1 ... done could not translate host name "db" to address: Name or service not known Couldn't create 'hoge_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>' /usr/local/bundle/gems/rake-13.0.1/exe/rake:27:in `<top (required)>' Tasks: TOP => db:create (See full trace by running task with --trace)

database.yml

default: &default adapter: postgresql encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: postgres password: password host: db development: <<: *default database: hoge_development test: <<: *default database: hoge_test production: <<: *default adapter: postgresql encoding: unicode pool: 5

【補足情報】
docker-compose.yml

version: '3' services: db: build: context: . dockerfile: ./.docker/postgresql/Dockerfile volumes: - ./.docker/postgresql/volumes:/var/lib/postgresql/data environment: - DATABASE_USER=postgres - DATABASE_PASSWORD=password web: tty: true stdin_open: true build: context: . dockerfile: ./.docker/rails/Dockerfile command: bundle exec foreman start -f Procfile.dev ports: - "3000:3000" - "8000:8000" volumes: - .:/hoge - ./.docker/rails/volumes:/usr/local/bundle environment: - DATABASE_USER=postgres - DATABASE_PASSWORD=password - BUNDLE_APP_CONFIG="/hoge/.bundle" depends_on: - db

解決方法をご教授ください。
よろしくお願い致します。

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

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

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

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

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

mit0223

2020/05/04 07:24 編集

おそらく、前の質問と同じとは思いますが、念の為、docker-compose.yml を貼ってもらえますでしょうか。
dxo

2020/05/04 07:37

ご返答いただき誠にありがとうございます。 docker-compose.ymlの方、追記させていただきました。 ご確認いただけますと幸いです。
guest

回答1

0

ベストアンサー

Could not translate host name "db" to address: Name or service not known

docker-compose には、 web と db の2個のコンテナが定義されていますが、 web のコンテナだけを起動しているので、 db のコンテナと通信しようとしても起動しておらず、通信することができません。

最終的には、docker-compose up -d で両方のコンテナを起動できるようにすべきです。そのためには、rails db:create RAILS_ENV=developmentのような初期化コマンドはENTORYPOINT でヘルパースクリプトを作成する必要があるでしょう。

しかし、コマンド単位でデバッグするのであれば、

docker-compose up -d db

で db コンテナを起動しておいて、

docker-compose run --rm web rails db:create RAILS_ENV=development

を実行してみてください。終わった後は、

docker-compose down

でコンテナを落として削除してください。


2020/5/5 コメントを受けて追記
dbコンテナが起動しない問題について

db_1 | Error: Database is uninitialized and superuser password is not specified.

db_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the
db_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

というエラーが出ています。dockerhub の postgres の READMEにもあるように postgres のコンテナでは POSTGRES_PASSWORD は必須となっています。また、ユーザIDはPOSTGRES_USER という環境変数で指定することになっています。

docker-compose.yml のdbコンテナの環境変数指定部分を修正してください。
修正前:

Dockerfile

1 db: 2 build: 3 context: . 4 dockerfile: ./.docker/postgresql/Dockerfile 5 volumes: 6 - ./.docker/postgresql/volumes:/var/lib/postgresql/data 7 environment: 8 - DATABASE_USER=postgres 9 - DATABASE_PASSWORD=password

修正後:

Dockerfile

1 db: 2 build: 3 context: . 4 dockerfile: ./.docker/postgresql/Dockerfile 5 volumes: 6 - ./.docker/postgresql/volumes:/var/lib/postgresql/data 7 environment: 8 - POSTGRES_USER=postgres 9 - POSTGRES_PASSWORD=password

投稿2020/05/04 08:03

編集2020/05/05 07:43
mit0223

総合スコア3401

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

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

dxo

2020/05/05 03:17 編集

ご返答いただきありがとうございます。 今回は、コマンド単位で、試し、 docker-compose run --rm web rails db:create RAILS_ENV=development こちらを叩いたあと、エラーが発生しております。 お手数おかけして申し訳ございませんが、ご確認のほどよろしくお願い致します。 Starting hoge-master_db_1 ... done could not translate host name "db" to address: Name or service not known Couldn't create hoge_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.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>' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/commands/rake/rake_command.rb:20:in `perform' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-6.0.2.1/lib/rails/commands.rb:18:in `<main>' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `block in require' /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:291:in `load_dependency' /usr/local/bundle/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `require' /hoge/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)>' /hoge/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)
mit0223

2020/05/04 11:06

db コンテナは正常に起動していますでしょうか?コマンド実行前の docker-compose ps の結果を貼ってもらえますでしょうか。
dxo

2020/05/04 11:18

ご返答並びにご確認いただき誠にありがとうございます。 以下、結果を貼らせていただきます。ご確認のほどよろしくお願い致します。 Name Command State Ports ----------------------------------------------------------------------------------------------------------------- hoge-master_db_1 docker-entrypoint.sh postgres Exit 1 hoge-master_web_1 entrypoint.sh bundle exec ... Up 0.0.0.0:3000->3000/tcp, 0.0.0.0:8000->8000/tcp
mit0223

2020/05/04 22:50 編集

> postgres Exit 1 となっているので、コンテナの起動に失敗しています。 docker-compose logs db の結果を貼ってください。できれば、質問を編集して貼ってください。
dxo

2020/05/05 03:19

ご返答ありがとうございます。承知致しました。 こちら、結果を貼らせていただきます。どうぞよろしくお願い致します。 Attaching to hoge-master_db_1 db_1 | Error: Database is uninitialized and superuser password is not specified. db_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the db_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". db_1 | db_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all db_1 | connections without a password. This is *not* recommended. db_1 | db_1 | See PostgreSQL documentation about "trust": db_1 | https://www.postgresql.org/docs/current/auth-trust.html db_1 | Error: Database is uninitialized and superuser password is not specified. db_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the db_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". db_1 | db_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all db_1 | connections without a password. This is *not* recommended. db_1 | db_1 | See PostgreSQL documentation about "trust": db_1 | https://www.postgresql.org/docs/current/auth-trust.html db_1 | Error: Database is uninitialized and superuser password is not specified. db_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the db_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". db_1 | db_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all db_1 | connections without a password. This is *not* recommended. db_1 | db_1 | See PostgreSQL documentation about "trust": db_1 | https://www.postgresql.org/docs/current/auth-trust.html db_1 | Error: Database is uninitialized and superuser password is not specified. db_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the db_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". db_1 | db_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all db_1 | connections without a password. This is *not* recommended. db_1 | db_1 | See PostgreSQL documentation about "trust": db_1 | https://www.postgresql.org/docs/current/auth-trust.html
dxo

2020/05/05 08:19

2度もご回答いただき誠にありがとうございました。 大変お手数おかけしました。今回ご指摘いただきました件、参照リンク等を改めて拝見させていただき、しっかり復習しておこうと思いました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問