前提・実現したいこと
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
1# PostgreSQL. Versions 9.3 and up are supported. 2# 3# Install the pg driver: 4# gem install pg 5# On macOS with Homebrew: 6# gem install pg -- --with-pg-config=/usr/local/bin/pg_config 7# On macOS with MacPorts: 8# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config 9# On Windows: 10# gem install pg 11# Choose the win32 build. 12# Install PostgreSQL and put its /bin directory on your path. 13# 14# Configure Using Gemfile 15# gem 'pg' 16# 17default: &default 18 adapter: postgresql 19 encoding: unicode 20 # For details on connection pooling, see Rails configuration guide 21 # https://guides.rubyonrails.org/configuring.html#database-pooling 22 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 23 host: db 24 username: postgres 25 password: "" 26 27development: 28 <<: *default 29 database: baukis2_development 30 31 # The specified database role being used to connect to postgres. 32 # To create additional roles in postgres see `$ createuser --help`. 33 # When left blank, postgres will use the default role. This is 34 # the same name as the operating system user that initialized the database. 35 #username: baukis2 36 37 # The password associated with the postgres role (username). 38 #password: 39 40 # Connect on a TCP socket. Omitted by default since the client uses a 41 # domain socket that doesn't need configuration. Windows does not have 42 # domain sockets, so uncomment these lines. 43 #host: localhost 44 45 # The TCP port the server listens on. Defaults to 5432. 46 # If your server runs on a different port number, change accordingly. 47 #port: 5432 48 49 # Schema search path. The server defaults to $user,public 50 #schema_search_path: myapp,sharedapp,public 51 52 # Minimum log levels, in increasing order: 53 # debug5, debug4, debug3, debug2, debug1, 54 # log, notice, warning, error, fatal, and panic 55 # Defaults to warning. 56 #min_messages: notice 57 58# Warning: The database defined as "test" will be erased and 59# re-generated from your development database when you run "rake". 60# Do not set this db to the same as development or production. 61test: 62 <<: *default 63 database: baukis2_test 64 65# As with config/credentials.yml, you never want to store sensitive information, 66# like your database password, in your source code. If your source code is 67# ever seen by anyone, they now have access to your database. 68# 69# Instead, provide the password as a unix environment variable when you boot 70# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database 71# for a full rundown on how to provide these environment variables in a 72# production deployment. 73# 74# On Heroku and other platform providers, you may have a full connection URL 75# available as an environment variable. For example: 76# 77# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" 78# 79# You can use this database configuration with: 80# 81# production: 82# url: <%= ENV['DATABASE_URL'] %> 83# 84production: 85 <<: *default 86 database: baukis2_production 87 username: baukis2 88 password: <%= ENV['BAUKIS2_DATABASE_PASSWORD'] %> 89
docker
1version: '3' 2services: 3 db: 4 image: postgres:11.2-alpine 5 volumes: 6 - ./tmp/db:/var/lib/postgresql/data 7 web: 8 build: . 9 command: /bin/sh 10 environment: 11 WEBPACKER_DEV_SERVER_HOST: "0.0.0.0" 12 RAILS_SERVE_STATIC_FILES: "1" 13 EDITOR: "vim" 14 volumes: 15 - ./apps:/apps 16 ports: 17 - "3000:3000" 18 - "3035:3035" 19 depends_on: 20 - db 21 tty: true 22
Dockerfile
1FROM oiax/rails6-deps:latest 2 3ARG UID=1000 4ARG GID=1000 5 6RUN mkdir /var/mail 7RUN groupadd -g $GID devel 8RUN useradd -u $UID -g devel -m devel 9RUN echo "devel ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers 10 11WORKDIR /tmp 12COPY init/Gemfile /tmp/Gemfile 13COPY init/Gemfile.lock /tmp/Gemfile.lock 14RUN bundle install 15 16COPY ./apps /apps 17 18RUN apk add --no-cache openssl 19 20USER devel 21 22RUN openssl rand -hex 64 > /home/devel/.secret_key_base 23RUN echo $'export SECRET_KEY_BASE=$(cat /home/devel/.secret_key_base)' \ 24 >> /home/devel/.bashrc 25 26WORKDIR /apps 27
試したこと
- docker-compose run web rails db:createを実行したが、変化はなかった。
- config/database.ymlのuserをrootにし、実行したが、変化はなかった。
- config/database.ymlのhostをlocalhostにし、実行したが、変化はなかった。
postgresqlのプロセスを確認したところ
存在していなかった
ps.sh
1ps -ef | grep ps -ef | grep post 2devel 1407 1333 0 09:46 pts/1 00:00:00 grep post
postgresqlに関するディレクトリが存在するか確認したところ
複数件ヒットした。
しかし、どのようにしてサービスのステータスを確認および、起動させるかわからず
調査中
grep.sh
1find / -type f | xargs grep postgres
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/26 15:34