【Rails Docker mysql】環境構築でハマったこと

DockerDocker

以下のようなエラーが出ました。

web_1  | Could not find gem 'mysql2 (>= 0.4.4, < 0.6.0)' in any of the gem sources listed in your Gemfile.
web_1  | Run `bundle install` to install missing gems.
sampleapp_web_1 exited with code 7
スポンサーリンク

Rubyのバージョンを確認

gemがないと怒られ、「bundle install」を実行しろと言われたので、実行したところ

$ bundle install
Your Ruby version is 2.6.3, but your Gemfile specified 2.5.8

rubyのバージョンを確認しました。

$ rbenv versions
rbenv: version `ruby-2.5.8' is not installed (set by /Users/user/desktop/work/docker/sampleApp/.ruby-version)
  system

インストールできるバージョンを確認

user-no-MacBook-Pro:sampleApp user$ rbenv install -l
2.5.8
2.6.6
2.7.1
jruby-9.2.13.0
maglev-1.0.0
mruby-2.1.2
rbx-5.0
truffleruby-20.2.0
truffleruby+graalvm-20.2.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all' to show all local versions.

バージョンを指定してインストールします。

$ rbenv install 2.5.8

バージョンを確認

$ rbenv versions
  system
* 2.5.8 (set by /Users/user/Desktop/work/docker/sampleApp/.ruby-version)

変更を確定させます。

$ source ~/.bash_profile

同様のエラーが出ました…..

rbenvの導入

Ruby Error[Your Ruby version is 2.6.3, but your Gemfile specified 2.6.5] - Qiita
Ruby Error上記のエラーに引っ掛かったので記事にしました。環境macOS Catalina 10.15.…

上記を参考にして確認

$ which bundler
/usr/bin/bundler
$ cat /usr/bin/bundler
#!/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'bundler' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0.a"

str = ARGV.first
if str
  str = str.b[/\A_(.*)_\z/, 1]
  if str and Gem::Version.correct?(str)
    version = str
    ARGV.shift
  end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('bundler', 'bundler', version)
else
gem "bundler", version
load Gem.bin_path("bundler", "bundler", version)
end

こちらがバージョン2.6のままですね。。。

Your Ruby version is 2.5.3, but your Gemfile specified 2.5.1 - Qiita
TL;DRとある日の朝、RORのプロジェクトを開発環境で立ち上げようとしたところ、こんなエラーが吐かれた。そのデバッグ記録です。Debugいつものように開発環境のRORプロジェクトをgem f…

rbenvのフォルダーを確認します。

$ rbenv which ruby
/Users/user/.rbenv/versions/2.5.8/bin/ruby

変更しようとしましたが、以下でも変更できません。

$ sudo vim /usr/bin/bundler

bundlerをインストールしてみました。

gem install bundler

すみませんが、bundlerを変更できなかったので、Gemfileのrubyバージョンを変更しました。。。
悪手な気もしますが。。。

bundle install

今度は無事インストール開始しましたが、またエラー。。。

An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.

mysql2をインストール

gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/user/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/user/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-19/2.5.0/mysql2-0.5.3/gem_make.out

またエラー。。。
rbenvのバージョンを変えてみる。

$ brew update
$ brew upgrade rbenv ruby-build
$ rbenv install -l
2.5.8
2.6.6
2.7.2
jruby-9.2.13.0
maglev-1.0.0
mruby-2.1.2
rbx-5.0
truffleruby-20.2.0
truffleruby+graalvm-20.2.0
$ rbenv install 2.6.6

なんか色々やってみたが、

$ rbenv rehash
$ bundle install

で正常にできた。。。
疲れたのでまた書きます。

今度はmysqlでエラーが

checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... no
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/8.0.21_1/lib
-----
creating Makefile

current directory: /Users/user/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/user/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/user/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/user/.rbenv/versions/2.5.8/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-19/2.5.0/mysql2-0.5.3/gem_make.out

以下を確認openssl

$ brew info openssl
openssl@1.1: stable 1.1.1g (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/usr/local/Cellar/openssl@1.1/1.1.1g (8,059 files, 18MB)
  Poured from bottle on 2020-10-05 at 00:23:35
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@1.1.rb
License: OpenSSL
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> /Users/user/.bash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

以下の部分を参考にセットする。

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

以下のように実施

$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/openssl@1.1/include"
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl@1.1/lib"
You are replacing the current local value of build.mysql2, which is currently "--with-cppflags=-I/usr/local/opt/openssl@1.1/include"

再度up

$ docker-compose up
Starting sampleapp_db_1 ... done
Starting sampleapp_web_1 ... done
Attaching to sampleapp_db_1, sampleapp_web_1
db_1   | 2020-10-06 23:41:10+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.31-1debian10 started.
db_1   | 2020-10-06 23:41:11+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1   | 2020-10-06 23:41:11+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.31-1debian10 started.
db_1   | 2020-10-06T23:41:12.530172Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1   | 2020-10-06T23:41:12.550709Z 0 [Note] mysqld (mysqld 5.7.31) starting as process 1 ...
db_1   | 2020-10-06T23:41:12.560813Z 0 [Warning] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
db_1   | 2020-10-06T23:41:12.570442Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1   | 2020-10-06T23:41:12.570531Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1   | 2020-10-06T23:41:12.570550Z 0 [Note] InnoDB: Uses event mutexes
db_1   | 2020-10-06T23:41:12.570591Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1   | 2020-10-06T23:41:12.570616Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db_1   | 2020-10-06T23:41:12.570642Z 0 [Note] InnoDB: Using Linux native AIO
db_1   | 2020-10-06T23:41:12.573055Z 0 [Note] InnoDB: Number of pools: 1
db_1   | 2020-10-06T23:41:12.573910Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1   | 2020-10-06T23:41:12.586452Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1   | 2020-10-06T23:41:12.634458Z 0 [Note] InnoDB: Completed initialization of buffer pool
db_1   | 2020-10-06T23:41:12.651662Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1   | 2020-10-06T23:41:12.665088Z 0 [ERROR] InnoDB: The Auto-extending innodb_system data file './ibdata1' is of a different size 64 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pages!
db_1   | 2020-10-06T23:41:12.665164Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
web_1  | You must use Bundler 2 or greater with this lockfile.
db_1   | 2020-10-06T23:41:13.266613Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
db_1   | 2020-10-06T23:41:13.266669Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
db_1   | 2020-10-06T23:41:13.266685Z 0 [ERROR] Failed to initialize builtin plugins.
db_1   | 2020-10-06T23:41:13.266694Z 0 [ERROR] Aborting
db_1   |
db_1   | 2020-10-06T23:41:13.266701Z 0 [Note] Binlog end
db_1   | 2020-10-06T23:41:13.266795Z 0 [Note] Shutting down plugin 'CSV'
db_1   | 2020-10-06T23:41:13.270234Z 0 [Note] mysqld: Shutdown complete
db_1   |
sampleapp_web_1 exited with code 20
sampleapp_db_1 exited with code 1

以下のエラーです。

You must use Bundler 2 or greater with this lockfile.

バージョンを確認してみました。

$ gem list bundler

*** LOCAL GEMS ***

bundler (2.1.4)

問題なさそうでうね。。。
アップデートしてみました。

$ gem install bundler
Successfully installed bundler-2.1.4
Parsing documentation for bundler-2.1.4
Done installing documentation for bundler after 3 seconds
1 gem installed
$ gem update --system

変わらず。。。

今度は下記を実施

bundle update --bundler

再度Gemfile.lockを削除し、bundle installを実施せずにdocker-compose buildをすると成功!!!

しかし今度はまたCould not find gem ‘mysql2 (>= 0.4.4, < 0.6.0)’ in any of the gem sources listed in your Gemfile.,,,,

bundlerをダウングレードしてみました。

$ gem install bundler -v 1.17.3
$ rm Gemfile.lock
$ bundle _1.17.3_ install

するとビルドまではいくがアップ時に以下のエラー

Could not find activesupport-5.2.4.4 in any of the sources

進まないので、新しいフォルダ作って最初からやってみます!

エンジニア転職なら【Tech Stars Agent】

フリーランスを目指すなら【テックパートナーズカレッジ】

未経験者向けWEBスクール【SHElikes】

最初から

同様にフォルダを作り、以下を準備する。

  • Dockerfile
  • Gemfile
  • docker-compose.yml

以下のコマンドを実施する。

$ docker-compose run web rails new . --force --database=mysql --skip-bundle

ビルドする。

$ docker-compose build

ここで以下のエラー

web_1  | Could not find gem 'mysql2 (>= 0.4.4, < 0.6.0)' in any of the gem sources listed in your Gemfile.
web_1  | Run `bundle install` to install missing gems.

以下を実施

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

確認する。

bundle show | grep mysql2
[DEPRECATED] use `bundle list` instead of `bundle show`
  * mysql2 (0.5.3)

再度ビルドすると今度はこちらのエラー,,,

web_1  | You must use Bundler 2 or greater with this lockfile.
dokosumu_web_1 exited with code 20

以下の記事をみて

docker-compose buildでYou must use Bundler 2 or greater with this lockfile.とエラーが出た話 - Qiita
##経緯「既存の作成済のrailsアプリケーションにDockerを導入してみたい!!でも、いきなり導入していろいろ変なことになったら嫌だな」ということで、Railsチュートリアルの開発環境をDoc…

Dockerfileで以下を修正しました。

$ RUN  bundle install

$ RUN gem install bundler && bundle install

再度ビルドを実行しましたが変わらず。。。

またダウングレードしてみました。

rubyを2.5.8から2.6.3にアップデート

結局これで解決しました!!!!

長かった。。。

なのでやはり正しいバージョンで実行することが全て見たいですね。

エンジニア転職なら【Tech Stars Agent】

フリーランスを目指すなら【テックパートナーズカレッジ】

未経験者向けWEBスクール【SHElikes】

コメント

タイトルとURLをコピーしました