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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

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

Docker

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

Q&A

解決済

2回答

7404閲覧

初めての開発環境構築なのですが。。。

PartyKids

総合スコア65

Ruby on Rails

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

Docker

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

0グッド

0クリップ

投稿2017/03/30 13:42

編集2017/03/31 02:08

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 したら上記のエラーが出てしまいました。。。。

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

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

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

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

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

guest

回答2

0

以前は、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.っと書かれていたので、中身を全て決して実行したら上手くいきました。

なぜか上手くいきましたが、原因が分からないので、分かる方いましたらよろしくお願いいたします。

投稿2017/03/31 01:30

PartyKids

総合スコア65

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

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

0

ベストアンサー

外しているかもしれませんが・・・

<database.yml(抜粋)>

development: &default adapter: postgresql encoding: unicode database: myapp_development pool: 5 username: postgres password: host: db

「host: db」 の指定が正しくないと思われます。

「host: localhost」 に変更して、rails testを実行すると、どうなりますでしょうか。

development: &default adapter: postgresql encoding: unicode database: myapp_development pool: 5 username: postgres password: host: localhost

投稿2017/03/30 14:15

gorogoroIT

総合スコア447

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

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

PartyKids

2017/03/30 14:38

回答ありがとうございます! 試してみましたが、上手くいきませんでした。。。 が、貴重なご意見ありがとうございます! 素人は、どこを見ればいいのか分からないので、見るべきポイントが1つ分かったので、良かったです!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問