Dockerでrails+MYSQLの環境構築およびdocker-compose run web rake db:createを実行
解決したいこと
Dockerでrails+MYSQLの環境構築。
ruby 2.5.3
rails 5.2.4
MYSQL 5.7
windows home
発生している問題・エラー
ec2-user:~/environment/rails-app (master) $ docker-compose run web rake db:create
Starting rails-app_db_1 ... done
Unknown MySQL server host 'db' (-2)
Couldn't create 'app_development' database. Please check your configuration.
rake aborted!
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_handling.rb:90:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/mysql_database_tasks.rb:6:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/mysql_database_tasks.rb:14:in `create'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:119:in `create'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:313:in `each'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:138:in `create_current'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/rake-13.0.3/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
【database.yml】
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
host: db
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
username: app
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
Dockerfile↓
FROM ruby:2.5.3
ENV LANG C.UTF-8
RUN apt-get update -qq && apt-get install -y build-essential nodejs
# yarnパッケージ管理ツールをインストール
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn
# Node.jsをインストール
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
apt-get install nodejs
WORKDIR /tmp
COPY Gemfile Gemfile.lock ./
RUN bundle install
WORKDIR /app
COPY . /app
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
Gemfile↓
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.4', '>= 5.2.4.5'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
docker-compose.yml↓
version: '3.3'
services:
db:
image: mysql:5.7
environment:
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbuser
expose:
- "5432"
ports:
- "5433:5432"
volumes:
- ./tmp/db:/var/lib/mysql/data
web:
build:
context: .
dockerfile: Dockerfile
command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
ports:
- "3000:3000"
environment:
MYSQL_HOST: db
MYSQL_USER: dbuser
MYSQL_PASSWORD: ''
MYSQL_PORT: 5432
volumes:
- .:/app
- bundle:/bundle
depends_on:
- db
tty: true
stdin_open: true
volumes:
bundle:
driver: local
entrypoint.sh↓
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /rails-app/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
自分で試したこと
次の記事を参照にして
https://gist.github.com/riouruma
https://qiita.com/seweb14wm54/items/4306506617ddba199eaa
https://qiita.com/kodai_0122/items/795438d738386c2c1966
Dockerに関するファイルを作成後、
docker-compose run web bundle exec rake db:createで
データベースの作成を実行しましたが、
$ docker-compose run web rake db:create
Starting rails-app_db_1 ... done
Unknown MySQL server host 'db' (-2)
Couldn't create 'app_development' database. Please check your configuration.
rake aborted!
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)
とエラーが返ってきました。
直訳すると
「不明なMySQLサーバーホスト 'db'(-2)
'app_development'データベースを作成できませんでした。構成を確認してください。
レーキが中止されました!
Mysql2 :: Error :: ConnectionError:不明なMySQLサーバーホスト 'db'(-2)」
MYSQサーバーホストが不明なようです。
database.ymlのhostをdbにしております。
他、この記事を参照しましたがdocker-compose.ymlの内容が間違って
エラーが生じてしまったのかと推測しております。
https://qiita.com/isacRU/items/dda2f2d9511c3a61854e
実現したいこと
$ docker-compose run web rake db:createが実行できるようになること。
アドバイス・ご指摘いただけますと幸甚に存じます。
よろしくお願い申し上げます。