状況
以下の内容のDockerfileでdocker build -t <イメージ名> .
実行後、docker run <イメージ名> rails new .
したところ以前は発生しなかったNameError: uninitialized constant Gem::Source
が発生しました。
FROM ruby:3.1.1
ARG APP_NAME=myapp
ARG USER_NAME=myuser
ENV TZ=Asia/Tokyo
WORKDIR /${APP_NAME}
COPY Gemfile /${APP_NAME}/Gemfile
COPY Gemfile.lock /${APP_NAME}/Gemfile.lock
RUN bundle install
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
RUN adduser ${USER_NAME} && \
chown -R ${USER_NAME} /${APP_NAME} && \
chown -R ${USER_NAME} ${GEM_HOME}
USER ${USER_NAME}
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
[12, #<Thread:0x0000ffffa5667c90 run>, #<NameError: uninitialized constant Gem::Source
(defined?(@source) && @source) || Gem::Source::Installed.new
^^^^^^^^
Did you mean? Gem::SourceList>, ["/usr/local/lib/ruby/3.1.0/bundler/rubygems_ext.rb:18:in `source'", "/usr/local/lib/ruby/3.1.0/bundler/rubygems_ext.rb:50:in `extension_dir'", "/usr/local/lib/ruby/3.1.0/rubygems/basic_specification.rb:339:in `have_file?'", "/usr/local/lib/ruby/3.1.0/rubygems/basic_specification.rb:86:in `contains_requirable_file?'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:1038:in `block (2 levels) in find_in_unresolved_tree'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2555:in `block (2 levels) in traverse'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2550:in `each'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2550:in `block in traverse'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2548:in `each'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2548:in `traverse'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:1034:in `block in find_in_unresolved_tree'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:1033:in `each'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:1033:in `find_in_unresolved_tree'", "<internal:/usr/local/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:114:in `require'", "/usr/local/lib/ruby/3.1.0/bundler/rubygems_ext.rb:18:in `source'", "/usr/local/lib/ruby/3.1.0/bundler/rubygems_ext.rb:50:in `extension_dir'", "/usr/local/lib/ruby/3.1.0/rubygems/basic_specification.rb:339:in `have_file?'", "/usr/local/lib/ruby/3.1.0/rubygems/basic_specification.rb:86:in `contains_requirable_file?'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:1038:in `block (2 levels) in find_in_unresolved_tree'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2555:in `block (2 levels) in traverse'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2550:in `each'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2550:in `block in traverse'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2548:in `each'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:2548:in `traverse'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:1034:in `block in find_in_unresolved_tree'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:1033:in `each'", "/usr/local/lib/ruby/3.1.0/rubygems/specification.rb:1033:in `find_in_unresolved_tree'", "<internal:/usr/local/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:114:in `require'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb:1:in `<top (required)>'", "/usr/local/lib/ruby/3.1.0/bundler/vendored_persistent.rb:11:in `require_relative'", "/usr/local/lib/ruby/3.1.0/bundler/vendored_persistent.rb:11:in `<top (required)>'", "/usr/local/lib/ruby/3.1.0/bundler/fetcher.rb:3:in `require_relative'", "/usr/local/lib/ruby/3.1.0/bundler/fetcher.rb:3:in `<top (required)>'", "<internal:/usr/local/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'", "<internal:/usr/local/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'", "/usr/local/lib/ruby/3.1.0/bundler/cli/install.rb:50:in `run'", "/usr/local/lib/ruby/3.1.0/bundler/cli.rb:255:in `block in install'", "/usr/local/lib/ruby/3.1.0/bundler/settings.rb:131:in `temporary'", "/usr/local/lib/ruby/3.1.0/bundler/cli.rb:254:in `install'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'", "/usr/local/lib/ruby/3.1.0/bundler/cli.rb:31:in `dispatch'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'", "/usr/local/lib/ruby/3.1.0/bundler/cli.rb:25:in `start'", "/usr/local/lib/ruby/gems/3.1.0/gems/bundler-2.3.7/libexec/bundle:48:in `block in <main>'", "/usr/local/lib/ruby/3.1.0/bundler/friendly_errors.rb:103:in `with_friendly_errors'", "/usr/local/lib/ruby/gems/3.1.0/gems/bundler-2.3.7/libexec/bundle:36:in `<main>'"]]
[12, #<Thread:0x0000ffffa5667c90 run>, #<RuntimeError: CRITICAL: RUBYGEMS_ACTIVATION_MONITOR.owned?: before false -> after true>, ["<internal:/usr/local/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:167:in `ensure in require'", "<internal:/usr/local/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:167:in `require'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb:1:in `<top (required)>'", "/usr/local/lib/ruby/3.1.0/bundler/vendored_persistent.rb:11:in `require_relative'", "/usr/local/lib/ruby/3.1.0/bundler/vendored_persistent.rb:11:in `<top (required)>'", "/usr/local/lib/ruby/3.1.0/bundler/fetcher.rb:3:in `require_relative'", "/usr/local/lib/ruby/3.1.0/bundler/fetcher.rb:3:in `<top (required)>'", "<internal:/usr/local/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'", "<internal:/usr/local/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'", "/usr/local/lib/ruby/3.1.0/bundler/cli/install.rb:50:in `run'", "/usr/local/lib/ruby/3.1.0/bundler/cli.rb:255:in `block in install'", "/usr/local/lib/ruby/3.1.0/bundler/settings.rb:131:in `temporary'", "/usr/local/lib/ruby/3.1.0/bundler/cli.rb:254:in `install'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'", "/usr/local/lib/ruby/3.1.0/bundler/cli.rb:31:in `dispatch'", "/usr/local/lib/ruby/3.1.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'", "/usr/local/lib/ruby/3.1.0/bundler/cli.rb:25:in `start'", "/usr/local/lib/ruby/gems/3.1.0/gems/bundler-2.3.7/libexec/bundle:48:in `block in <main>'", "/usr/local/lib/ruby/3.1.0/bundler/friendly_errors.rb:103:in `with_friendly_errors'", "/usr/local/lib/ruby/gems/3.1.0/gems/bundler-2.3.7/libexec/bundle:36:in `<main>'"]]
/usr/local/lib/ruby/3.1.0/bundler/rubygems_ext.rb:18:in `source': uninitialized constant Gem::Source (NameError)
(defined?(@source) && @source) || Gem::Source::Installed.new
^^^^^^^^
Did you mean? Gem::SourceList
from /usr/local/lib/ruby/3.1.0/bundler/rubygems_ext.rb:50:in `extension_dir'
from /usr/local/lib/ruby/3.1.0/rubygems/basic_specification.rb:339:in `have_file?'
from /usr/local/lib/ruby/3.1.0/rubygems/basic_specification.rb:86:in `contains_requirable_file?'
from /usr/local/lib/ruby/3.1.0/rubygems/specification.rb:1038:in `block (2 levels) in
<省略>
run bundle binstubs bundler
Could not find gem 'sprockets-rails' in locally installed gems.
rails importmap:install
Could not find gem 'sprockets-rails' in locally installed gems.
Run `bundle install` to install missing gems.
rails turbo:install stimulus:install
Could not find gem 'sprockets-rails' in locally installed gems.
Run `bundle install` to install missing gems.
解決した方法
Bundlerをアップデートするために、Dockerfileを以下のように修正し、再度docker build -t <イメージ名> .
実行後、docker run <イメージ名> rails new .
することで解消しました。
FROM ruby:3.1.1
ARG APP_NAME=myapp
ARG USER_NAME=myuser
ENV TZ=Asia/Tokyo
# 追記
ARG RUBYGEMS_VERSION=3.3.20
WORKDIR /${APP_NAME}
COPY Gemfile /${APP_NAME}/Gemfile
COPY Gemfile.lock /${APP_NAME}/Gemfile.lock
# RubyGemsをアップデート
RUN gem update --system ${RUBYGEMS_VERSION} && \
bundle install
# COPY した Gemfile.lock に BUNDLED WITH 2.3.7 または 2.3.8 と記述されている場合は
# bundle install の前に bundle update --bundler を実行して Gemfile.lock を更新する必要あり。
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
RUN adduser ${USER_NAME} && \
chown -R ${USER_NAME} /${APP_NAME} && \
chown -R ${USER_NAME} ${GEM_HOME}
USER ${USER_NAME}
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
ちなみに修正前のBundlerのバージョンは2.3.7
でした。
$ docker run <イメージ名> bundler -v
Bundler version 2.3.7
どうやらBundlerのバージョンが2.3.7
だとNameError: uninitialized constant Gem::Source
が発生するようです。
また、2.3.8
でも同様のエラーが発生しました。
2.3.6
や2.3.9
、2.3.10
ではエラーは発生しませんでした。
追記
再度GitHubのIssuesを確認したところ解消法としてgem update --system
およびbundle update --bundler
を実行することを推奨する旨コメントが追加されていました。
本記事の該当箇所についても修正いたしました。