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

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

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

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

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

Docker

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

Q&A

1回答

2838閲覧

dockerでのrspec実行時のエラー Unknown MySQL server host 'db' (-2)

yoohe0722

総合スコア4

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

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

Docker

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

0グッド

0クリップ

投稿2020/02/05 03:24

前提・実現したいこと

rspecの実行時に下記のエラーが出てしまっており、テストの実行ができません。
開発環境、本番環境ともに新規投稿やユーザー作成は行えており、テストの実行のみ行えない状況です。

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

Failure/Error: ActiveRecord::Migration.maintain_test_schema! Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)

該当のソースコード

rails_helper.rb # This file is copied to spec/ when you run 'rails generate rspec:install' require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../config/environment', __dir__) # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end # in _spec.rb will both be required and run as specs, causing the specs to be # run twice. It is recommended that you do not name files matching this glob to # end with _spec.rb. You can configure this pattern with the --pattern # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. # # The following line is provided for convenience purposes. It has the downside # of increasing the boot-up time by auto-requiring all files in the support # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } # Checks for pending migrations and applies them before tests are run. # If you are not using ActiveRecord, you can remove these lines. begin # ActiveRecord::Migration.maintain_test_schema! rescue ActiveRecord::PendingMigrationError => e puts e.to_s.strip exit 1 end RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. config.use_transactional_fixtures = true # RSpec Rails can automatically mix in different behaviours to your tests # based on their file location, for example enabling you to call `get` and # `post` in specs under `spec/controllers`. # # You can disable this behaviour by removing the line below, and instead # explicitly tag your specs with their type, e.g.: # # RSpec.describe UsersController, :type => :controller do # # ... # end # # The different available types are documented in the features, such as in # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! # Filter lines from Rails gems in backtraces. config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") end
docker-compose.yml version: '3' services: db: image: mysql:5.7 ports: - "4306:3306" env_file: db.env web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/app_name ports: - "3000:3000" depends_on: - db image: mysql:5.7 env_file: db.env tty: true stdin_open: true
database.yml # MySQL. Versions 5.1.10 and up are supported. # # Install the MySQL driver # gem install mysql2 # # Ensure the MySQL gem is defined in your Gemfile # gem 'mysql2' # # And be sure to use new-style password hashing: # https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html # default: &default adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: <%= ENV.fetch('DB_USERNAME', 'root') %> password: <%= ENV.fetch('DB_PASSWORD', 'password') %> socket: /tmp/mysql.sock host: <%= ENV.fetch('DB_HOST', 'db') %> development: <<: *default database: study-log_development # 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: study-log_test # As with config/secrets.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 http://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="mysql2://myuser:mypass@localhost/somedatabase" # # You can use this database configuration with: # # production: # url: <%= ENV['DATABASE_URL'] %> # production: <<: *default adapter: postgresql encoding: unicode pool: 5

試したこと

docker-composeに下記追加
web: image: mysql:5.7

database.ymlのhost部分を下記のように修正したがうまくいかず
host: db

rails_helper.rbの下記部分の記述コメントアウト
ActiveRecord::Migration.maintain_test_schema!

補足情報(FW/ツールのバージョンなど)

ruby '2.6.3' rails '5.2.3'

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

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

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

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

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

guest

回答1

0

MySQLにsocketで接続するような設定になっていますが、webコンテナの方にはsocketが存在しないので繋がらないと思います

以下のようにnamed volumeを使うと良いかと思います

yaml

1services: 2 db: 3 volumes: 4 - "socket-data:/tmp/mysql.sock" 5 web: 6 volumes: 7 - "socket-data:/tmp/mysql.sock" 8volumes: 9 socket-data

投稿2020/02/05 13:52

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

yoohe0722

2020/02/06 12:39 編集

回答ありがとうございます! docker-compose.ymlを下記のように編集したところ、 ``` docker-compose.yml version: '3' services: db: image: mysql:5.7 ports: - "4306:3306" env_file: db.env volumes: - "socket-data:/tmp/mysql.sock" web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - "socket-data:/tmp/mysql.sock" ports: - "3000:3000" depends_on: - db env_file: db.env tty: true stdin_open: true volumes: socket-data ``` docker-compose build を実行すると下記のエラーが出ました。 `ERROR: In file './docker-compose.yml', volume must be a mapping, not a string.` 調べたところ、文末に:が必要なようだったので、 `socket-data:` とすると無事build自体はできました。 しかし、rspecを実行すると下記のエラーが出て実行できませんでした。 `Unknown MySQL server host 'db' (0)` 他になにか考えられる原因はありますでしょうか?
退会済みユーザー

退会済みユーザー

2020/02/06 13:21

MySQLのsocketの場所が違うとかでしょうか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問