沖永凌のブログ

株式会社ARTISAN, 現場Hub, SaaS開発, スタートアップ, CTO

DockerfileのRubyのバージョンを上げた時に、bundle installが失敗する問題の解決策

前提

Railsアプリケーション(7.1.0)を動かすためのコンテナで遭遇したバグ。

Dockerfileに記載したRubyのバージョンのみを3.2.2から3.3.0に変更した時に発生。

事象

コンテナを起動しようとすると、以下のエラーを吐き出して、コンテナが終了する。

Ignoring psych-5.1.2 because its extensions are not built. Try: gem pristine psych --version 5.1.2
Ignoring psych-5.1.1.1 because its extensions are not built. Try: gem pristine psych --version 5.1.1.1
Ignoring psych-5.1.1 because its extensions are not built. Try: gem pristine psych --version 5.1.1
Ignoring stringio-3.1.0 because its extensions are not built. Try: gem pristine stringio --version 3.1.0
Ignoring stringio-3.0.9 because its extensions are not built. Try: gem pristine stringio --version 3.0.9
Ignoring stringio-3.0.8 because its extensions are not built. Try: gem pristine stringio --version 3.0.8
<internal:/usr/local/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:127:in `require': libruby.so.3.2: cannot open shared object file: No such file or directory - /usr/local/bundle/gems/psych-5.1.2/lib/psych.so (LoadError)

コンテナ外から

docker compose run app bundle install

を実行しても、同じエラーが発生。

原因

Gemfile.lockに書かれている&installされているbudlerやgemのバージョンは古いままだが、Rubyのバージョンだけが上がっており競合が起きた。

解消方法

一旦競合の起きているgemをuninstallし、再度bundle installをすると解決した。

1.Gemfile.lockを削除

2.コンテナ外から競合するgemをuninstall(上記のエラー文を参照してgemを特定)

docker compose run app gem uninstall psych
docker compose run app gem uninstall stringio

3.コンテナ外からbundle installを実行

docker compose run app bundle install