実務未経験の初学者です。Docker, Rails, MySQLで開発して、CircleCIでテストを実行しようとすると、エラーが出てしまいます。
Ruby2.5.1
Rails5.2
MySQL5.7
docker-compose1.24.1
Docker19.03.4
エラーメッセージ
#!/bin/bash -eo pipefail bundle exec rake db:create Unknown MySQL server host 'db' (-2) Couldn't create 'myapp_development' database. Please check your configuration. rake aborted! Mysql2::Error: Unknown MySQL server host 'db' (-2) /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:89:in `connect' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:89:in `initialize' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_handling.rb:118:in `retrieve_connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/connection_handling.rb:90:in `connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/mysql_database_tasks.rb:6:in `connection' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/mysql_database_tasks.rb:14:in `create' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:119:in `create' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:313:in `each' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/tasks/database_tasks.rb:138:in `create_current' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.4/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>' /home/circleci/repo/vendor/bundle/ruby/2.5.0/gems/rake-13.0.1/exe/rake:27:in `<top (required)>' /usr/local/bin/bundle:30:in `block in <main>' /usr/local/bin/bundle:22:in `<main>' Tasks: TOP => db:create (See full trace by running task with --trace) Exited with code 1
.circleci/config.yml
version: 2 jobs: build: docker: - image: circleci/ruby:2.5-node-browsers-legacy environment: RAILS_ENV: test DB_HOST: 127.0.0.1 - image: mysql:5.7 environment: MYSQL_USER: root MYSQL_ROOT_PASSWORD: ramadan working_directory: ~/repo steps: - checkout - restore_cache: keys: - v1-dependencies-{{ checksum "Gemfile.lock" }} - v1-dependencies- - run: name: install dependencies command: | bundle install --jobs=4 --retry=3 --path vendor/bundle - save_cache: paths: - ./vendor/bundle key: v1-dependencies-{{ checksum "Gemfile.lock" }} - run: bundle exec rake db:create - run: bundle exec rake db:schema:load - run: name: Update ChromeDriver Version command: | bundle exec rake webdrivers:chromedriver:update - run: name: Rspec command: | mkdir /tmp/test-results TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \ circleci tests split --split-by=timings)" bundle exec rspec \ --format progress \ --out /tmp/test-results/rspec.xml \ --format progress \ $TEST_FILES . . .
datebase.yml
default: &default adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: ramadan host: db development: <<: *default database: myapp_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: myapp_test
docker-compose
version: '3' services: rails: build: . ports: - "3000:3000" command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/myapp environment: - "SELENIUM_DRIVER_URL=http://selenium_chrome:4444/wd/hub" links: - db selenium_chrome: image: selenium/standalone-chrome-debug logging: driver: none depends_on: - db restart: unless-stopped db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: ramadan MYSQL_DATEBASE: root ports: - '3306:3306' volumes: - mysql_data:/var/lib/mysql volumes: mysql_data:
回答3件
あなたの回答
tips
プレビュー