LoginSignup
2
1

More than 3 years have passed since last update.

【Rails × Docker】*エラー* Could not find gem 'mysql2 (~> 0.5)' in any of the gem sources listed in your Gemfile.

Last updated at Posted at 2021-01-05

◆目的
ずっと悩んでいたDockerの課題解決に一歩近ずけたので忘却録のため、記述します!
*補足
私の場合、mysql2がないよ!というエラーが出た際、無理くり手動でmysql2を入れてみました。ですが、Gemfileのmysqlの次に記述されてるpumaがないですよ!というエラーが出たので、別の方法を探していたところwebpackerにたどり着きました。

◆結論
コンテナ上で「rails webpacker:install」このコマンドを打った後、
Gemfile.lockの中身を削除して、
docker-compose up --build コマンドを打ったらコンテナが起動しました^^

root@c9fe1cb41fda:/app2# rails webpacker:install

エラー文

docker run コマンドを入力
mysql2がgemのどのリストを探してもないとエラーがでる。
↓↓↓
Could not find gem 'mysql2 (~> 0.5)' in any of the gem sources listed in your Gemfile.
Run bundle install to install missing gems.

*初回のbuildで docker build . ではなく docker-compose  コマンドでimageを作成するべきだったのかも...

ユーザー名noMBP:app2 ユーザー名$ docker run -it -v /Users/ユーザー名/Desktop/app2:/app2 -p 3000:3000 f5dc18466a56 bash
root@c1f90c58d633:/app2# rails new . --force --database=mysql --skip-bundle
 〜 省略 〜
      create  storage
      create  storage/.keep
      create  tmp/storage
      create  tmp/storage/.keep
      remove  config/initializers/cors.rb
      remove  config/initializers/new_framework_defaults_6_1.rb
       rails  webpacker:install
Could not find gem 'mysql2 (~> 0.5)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

仮説1 Gemfileにmysqlの記述がないのかもしれない。

catコマンドでGemfileの中身を確認
↓↓↓
Gemfileに gem 'mysql2', '~> 0.5'  が存在している。

git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.0'
# Use mysql as the database for Active Record
gem 'mysql2', '~> 0.5'
# Use Puma as the app server
gem 'puma', '~> 5.0'
 〜 省略 〜

仮説2 webpackerに問題があるのではないか。

webpackerインストールの直後にエラーが出ているため、webpackerに問題があるのではないか。
コンテナでrails sしてみるとwebpackerのエラーが出ていてwebpackerを入れてねと言われていることがわかる。
Webpacker configuration file not found /app2/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /app2/config/webpacker.yml (RuntimeError)

root@c9fe1cb41fda:/app2# rails s
=> Booting Puma
=> Rails 6.1.0 application starting in development 
=> Run `bin/rails server --help` for more startup options
Exiting
Traceback (most recent call last):

 〜 省略 〜

     2: from /usr/local/bundle/gems/webpacker-5.2.1/lib/webpacker/configuration.rb:92:in `data'
     1: from /usr/local/bundle/gems/webpacker-5.2.1/lib/webpacker/configuration.rb:95:in `load'
/usr/local/bundle/gems/webpacker-5.2.1/lib/webpacker/configuration.rb:99:in `rescue in load': Webpacker configuration file not found /app2/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /app2/config/webpacker.yml (RuntimeError)

app2の中を確認→webpacker.ymlがない。

調べるとRails6以降はデフォルトでwebpackerが入っているということだったが、
なかったので、試行錯誤して入れてみることにした。

①Dockerfileへ記述してもう一度 build してみる「apt-get install -y webpacker'」
E: Unable to locate package webpacker
ERROR: Service 'web' failed to build : The command '/bin/sh -c apt-get install -y webpacker' returned a non-zero code: 100

ユーザー名noMBP:app2 ユーザー名$ docker-compose up --build
Building web
Step 1/24 : FROM ruby:2.7
 ---> 7e58098089a4
Step 2/24 : ENV BUNDLER_VERSION=2.1.4
 〜 省略 〜
Step 11/24 : RUN apt-get install -y  webpacker
 ---> Running in 283d9d5a7034
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package webpacker
ERROR: Service 'web' failed to build : The command '/bin/sh -c apt-get install -y  webpacker' returned a non-zero code: 100

②コンテナの中で以下のコマンドを実行

root@c9fe1cb41fda:/app2# rails webpacker:install

Done in 153.13s.
Webpacker successfully installed 🎉 🍰

成功!!

Gemfile.lockの中身を削除して docker-compose up --build を実行

無事に起動!!

web_1  | => Booting Puma
web_1  | => Rails 6.1.0 application starting in development 
web_1  | => Run `bin/rails server --help` for more startup options
web_1  | Puma starting in single mode...
web_1  | * Puma version: 5.1.1 (ruby 2.7.2-p137) ("At Your Service")
web_1  | *  Min threads: 5
web_1  | *  Max threads: 5
web_1  | *  Environment: development
web_1  | *          PID: 1
web_1  | * Listening on http://0.0.0.0:3000
web_1  | Use Ctrl-C to stop

まとめ

根本的な解決には至ってないですが、なんとかコンテナを起動させることができました。

1. docker runした際、なぜWebpackerがインストールされずmysqlのエラーが出たのか。
2. 直接コンテナでコマンドを打つことで起動させることができたが、次回起動時問題ないのか

上記2点引き続き調べていきたいと思います。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1