Could not find mysql2-0.5.3 in any of the sources Run `bundle install` to install missing gems.
解決したいこと
この記事 https://qiita.com/YK0214/items/0aaaf5b99cbde44f15ea
を参考にして RailsアプリのDocker化をしたいのですが、
一通りファイルを編集したあたりでDockerを起動しようとするとエラーが出てしまい進めません。
発生している問題・エラー
なにかコマンドを打つと
下記のメッセージが出てきてしまう。
Could not find mysql2-0.5.3 in any of the sources
Run `bundle install` to install missing gems.
自分で試したこと
bundle installしてもまた同じメッセージが出る。
下記の記事の解決方法を試して見たが状況が変わらない。
https://qiita.com/sun_peace/items/f06cc95e63a71b7296ec
コード
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.3'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.4', '>= 5.2.4.3'
# # Use sqlite3 as the database for Active Record
# gem 'sqlite3', '~> 1.3.6'
# gem 'mysql2', '~>0.5.3'
# 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'
gem 'rubocop-airbnb'
end
group :test do
gem 'capybara', '>= 2.15'
gem 'rspec-rails'
gem "factory_bot_rails"
gem 'faker'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'devise'
gem "refile", require: "refile/rails", github: 'manfe/refile'
gem "refile-mini_magick"
gem 'bootstrap', '~> 4.5'
gem 'bootstrap-sass', '~> 3.3.6'
gem 'jquery-rails'
gem 'font-awesome-sass', '~> 5.13'
gem 'kaminari'
gem 'kaminari-bootstrap', '~> 3.0.1'
gem 'dotenv-rails'
group :production do
gem 'mysql2'
end
gem 'carrierwave'
gem 'fog-aws'
gem 'rakuten_web_service'
Dockerfile
FROM ruby:2.6.3
# 必要なパッケージのインストール。node.jsについては当初の記述だとエラーが出たため、修正。
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y mariadb-client --no-install-recommends && rm -rf /var/lib/apt/lists/*
# 作業ディレクトリの作成、設定
RUN mkdir /workdir
WORKDIR /workdir
# ホスト側(ローカル)のGemfileを上記で作成した/workdirに追加する
ADD Gemfile /workdir/Gemfile
ADD Gemfile.lock /workdir/Gemfile.lock
# Gemfileのbundle install
# ENVなしで実行したところエラーが出た。BUNDLER_VERSIONを指定することで回避。
ENV BUNDLER_VERSION 2.1.4
RUN gem install bundler
RUN bundle install
# ホスト側(ローカル)の全てのディレクトリをDocekrコンテナの/workdir配下に追加。
ADD . /workdir
docker-compose.yml
# docker-composeのバージョンを指定します。今回は'3'を使用。
version: "3"
# 起動するコンテナの定義を行います。このファイルではdb, db-test , webの三つを定義しています。
services:
db:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password # デフォルトの認証形式をmysql_native_passwordに変更。MySQL5.0系なら不要。
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: appname_development
MYSQL_USER: root #任意のユーザー
MYSQL_PASSWORD: password
TZ: Asia/Tokyo
volumes:
- ./mysql/mysql_data:/var/lib/mysql
- ./logs:/var/log/mysql
- ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
ports:
- "4306:3306"
db-test:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: appname_test
MYSQL_USER: root
MYSQL_PASSWORD: password
TZ: Asia/Tokyo
volumes:
- ./mysql/my.cnf:/etc/mysql/conf.d/my.cnf
tmpfs:
- /var/lib/mysql
- /var/log/mysql
web:
build:
context: .
dockerfile: Dockerfile
command: /bin/sh -c "rm -f /workdir/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
tty: true
stdin_open: true
depends_on:
- db
ports:
- "3000:3000"
volumes:
- .:/workdir
config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: appname_development
username: root
password: password
host: db
socket: /tmp/mysql.sock
test:
<<: *default
database: appname_test
host: db-test
username: root
password: password
socket: /tmp/mysql.sock
production:
<<: *default
database: <%= ENV['DB_NAME'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
host: <%= ENV['DB_HOSTNAME'] %>
0