0

前提・実現したいこと

Ruby on Rails6 実践ガイドの書籍を用いてDockerでRuby on Railsの環境構築をしたいです。
以下のコマンドを実行するとエラーが出て、データベースに接続されないようです。解決策をお教え下さい。

発生している問題・エラーメッセージ

bash-4.4$ bin/rails db:create
could not translate host name "db" to address: Name does not resolve
Couldn't create 'baukis2_development' database. Please check your configuration.
rails aborted!
PG::ConnectionBad: could not translate host name "db" to address: Name does not resolve
/usr/local/bundle/gems/pg-1.2.3/lib/pg.rb:58:in `initialize'
/usr/local/bundle/gems/pg-1.2.3/lib/pg.rb:58:in `new'
/usr/local/bundle/gems/pg-1.2.3/lib/pg.rb:58:in `connect'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:46:in `postgresql_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:889:in `new_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:933:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:912:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:873:in `acquire_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:595:in `checkout'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:439:in `connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:1121:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_handling.rb:238:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/connection_handling.rb:206:in `connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/postgresql_database_tasks.rb:12:in `connection'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/postgresql_database_tasks.rb:21:in `create'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:126:in `create'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:185:in `block in create_current'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:476:in `each'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:475:in `each'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/tasks/database_tasks.rb:184:in `create_current'
/usr/local/bundle/gems/activerecord-6.0.2.2/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <main>'
/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/usr/local/bundle/gems/railties-6.0.2.2/lib/rails/command.rb:48:in `invoke'
/usr/local/bundle/gems/railties-6.0.2.2/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.2/lib/active_support/dependencies.rb:325:in `block in require'
/usr/local/bundle/gems/activesupport-6.0.2.2/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/bundle/gems/activesupport-6.0.2.2/lib/active_support/dependencies.rb:325:in `require'
/apps/baukis2/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)>'
/apps/baukis2/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)

該当のソースコード

database.yml

# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On macOS with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS 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
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: db
  username: postgres
  password: ""

development:
  <<: *default
  database: baukis2_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: baukis2

  # 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: baukis2_test

# As with config/credentials.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 https://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: baukis2_production
  username: baukis2
  password: <%= ENV['BAUKIS2_DATABASE_PASSWORD'] %>

docker-compose.yml

version: '3'
services:
  db:
    image: postgres:11.2-alpine
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: /bin/sh
    environment:
      WEBPACKER_DEV_SERVER_HOST: "0.0.0.0"
      RAILS_SERVE_STATIC_FILES: "1"
      EDITOR: "vim"
    volumes:
      - ./apps:/apps
    ports:
      - "3000:3000"
      - "3035:3035"
    depends_on:
      - db
    tty: true

Dockerfile

FROM oiax/rails6-deps:latest

ARG UID=1000
ARG GID=1000

RUN mkdir /var/mail
RUN groupadd -g $GID devel
RUN useradd -u $UID -g devel -m devel
RUN echo "devel ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

WORKDIR /tmp
COPY init/Gemfile /tmp/Gemfile
COPY init/Gemfile.lock /tmp/Gemfile.lock
RUN bundle install

COPY ./apps /apps

RUN apk add --no-cache openssl

USER devel

RUN openssl rand -hex 64 > /home/devel/.secret_key_base
RUN echo $'export SECRET_KEY_BASE=$(cat /home/devel/.secret_key_base)' \
  >> /home/devel/.bashrc

WORKDIR /apps

試したこと

  1. docker-compose run web rails db:create を実行したが、変化はなかった。
  2. config/database.ymlのuserをrootにし、実行したが、変化はなかった。
  3. config/database.ymlのhostをlocalhostにし、実行したが、変化はなかった。

postgresqlのプロセスを確認したところ
存在していなかった

ps -ef | grep ps -ef | grep post
devel     1407  1333  0 09:46 pts/1    00:00:00 grep post

postgresqlに関するディレクトリが存在するか確認したところ複数件ヒットした。
しかし、どのようにしてサービスのステータスを確認および、起動させるかわからず調査中。

find / -type f | xargs grep postgres

1 件の回答 1

1

could not translate host name "db" to address: Name does not resolve

とあるので、Docker内部DNSの名前解決ができていないように見えます。
アプリケーションのコンテナの中から ping db などは通りますか?

2
  • コメントありがとうございます! 取り急ぎご報告です。 sudo ping 127.0.0.1でまずpingが動作することを確認してから ping を飛ばしてみましたが、pingは通りませんでした。 ping.sh sudo ping db ping: bad address 'db'
    – yodev
    Commented 2020年4月25日 11:50
  • docker-compose ps でプロセスを確認したところ Name Command State Ports -------------------------------------------------------------------------------------------------------------- rails6-compose_db_1 docker-entrypoint.sh postgres Exit 1 rails6-compose_web_1 /bin/sh Up 0.0.0.0:3000->3000/tcp, 0.0.0.0:3035->3035/tcp とdbがExit状態になっておりました。 upしなければいけなさそうなのでこちらを調査しております。
    – yodev
    Commented 2020年4月25日 13:21

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。