os:sierra 10.12.4
rails5
ruby:2.3.3
docker公式チュートリアルquickstart:compose and rails
(https://docs.docker.com/compose/rails/)に沿って設定しています。
前回の質問で、なんとかlocalhost:3000でrails5の画面が表示、controllerやroutesの設定までいく事が出来ました。
しかし、
linux
1$rails test 2/Users/hahaha/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:671:in `initialize': could not translate host name "db" to address: nodename nor servname provided, or not known (PG::ConnectionBad)
っというエラーが出て解決方法が分かりません。
ruby
1databese.yml 2# PostgreSQL. Versions 9.1 and up are supported. 3# 4# Install the pg driver: 5# gem install pg 6# On OS X with Homebrew: 7# gem install pg -- --with-pg-config=/usr/local/bin/pg_config 8# On OS X with MacPorts: 9# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config 10# On Windows: 11# gem install pg 12# Choose the win32 build. 13# Install PostgreSQL and put its /bin directory on your path. 14# 15# Configure Using Gemfile 16# gem 'pg' 17# 18default: &default 19 adapter: postgresql 20 encoding: unicode 21 # For details on connection pooling, see rails configuration guide 22 # http://guides.rubyonrails.org/configuring.html#database-pooling 23 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 24 25development: &default 26 adapter: postgresql 27 encoding: unicode 28 database: myapp_development 29 pool: 5 30 username: postgres 31 password: 32 host: db 33 34 # The specified database role being used to connect to postgres. 35 # To create additional roles in postgres see `$ createuser --help`. 36 # When left blank, postgres will use the default role. This is 37 # the same name as the operating system user that initialized the database. 38 #username: myapp 39 40 # The password associated with the postgres role (username). 41 #password: 42 43 # Connect on a TCP socket. Omitted by default since the client uses a 44 # domain socket that doesn't need configuration. Windows does not have 45 # domain sockets, so uncomment these lines. 46 #host: localhost 47 48 # The TCP port the server listens on. Defaults to 5432. 49 # If your server runs on a different port number, change accordingly. 50 #port: 5432 51 52 # Schema search path. The server defaults to $user,public 53 #schema_search_path: myapp,sharedapp,public 54 55 # Minimum log levels, in increasing order: 56 # debug5, debug4, debug3, debug2, debug1, 57 # log, notice, warning, error, fatal, and panic 58 # Defaults to warning. 59 #min_messages: notice 60 61# Warning: The database defined as "test" will be erased and 62# re-generated from your development database when you run "rake". 63# Do not set this db to the same as development or production. 64test: 65 <<: *default 66 database: myapp_test 67 68# As with config/secrets.yml, you never want to store sensitive information, 69# like your database password, in your source code. If your source code is 70# ever seen by anyone, they now have access to your database. 71# 72# Instead, provide the password as a unix environment variable when you boot 73# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database 74# for a full rundown on how to provide these environment variables in a 75# production deployment. 76# 77# On Heroku and other platform providers, you may have a full connection URL 78# available as an environment variable. For example: 79# 80# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" 81# 82# You can use this database configuration with: 83# 84# production: 85# url: <%= ENV['DATABASE_URL'] %> 86# 87production: 88 <<: *default 89 database: myapp_production 90 username: myapp 91 password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
コメントにある
ruby
1# Install the pg driver: 2# gem install pg 3# On OS X with Homebrew: 4# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
は実行いたしました。
ググって以下の事をしてもエラーが出てしまいました。
ruby
1$rails db:migrate 2rake aborted! 3PG::ConnectionBad: could not translate host name "db" to address: nodename nor servname provided, or not known
どこがダメなのか分からず、丸投げな質問になってしまって申し訳ないのですが、よろしくお願いいたします。
追記
以前は、home/users/****/にworkspaceというファイルを作成し、その下でDockerfile等を作成をして環境構築していました。
Dockerfile FROM ruby:2.3.3 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN mkdir /myapp WORKDIR /myapp ADD Gemfile /myapp/Gemfile ADD Gemfile.lock /myapp/Gemfile.lock RUN bundle install ADD . /myapp
mkdir /myappというのが引っかかったので、workspace内にmyappフォルダがあるか調べたがなかったので、workspaceをmyappに変更して構築したら、上手くいきました。
また、お試しでRubymineを使っているのですが、Gemfileを作成時に自動的にGemfile.lockが作成され、空ファイルではなくgemがたくさん書かれていました。
公式ではYou’ll need an empty Gemfile.lock in order to build our Dockerfile.っと書かれていたので、中身を全て決して実行したら上手くいきました。
なぜか上手くいきましたが、原因が分からないので、分かる方いましたらよろしくお願いいたします。
###追記
A server is already running. Check /myapp/tmp/pids/server.pid.
っと出たので、server.pidを削除し、docker-compose up してから、rails test したら上記のエラーが出てしまいました。。。。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。