Skip to content

Instantly share code, notes, and snippets.

@sephraim
Last active April 2, 2024 20:21
Show Gist options
  • Save sephraim/b11ff4d704c4f7d13280c2b3603cbea4 to your computer and use it in GitHub Desktop.
Save sephraim/b11ff4d704c4f7d13280c2b3603cbea4 to your computer and use it in GitHub Desktop.
[Resolving "bundler requires Ruby version >= 3.0.0" in Dockerfile]

If you try to build your Ruby Dockerfile and get the following error(s):

 => ERROR [app base 4/4] RUN gem install bundler                                                                                                                                                                                                  24.1s
------
 > [app base 4/4] RUN gem install bundler:
24.10 ERROR:  Error installing bundler:
24.10 	The last version of bundler (>= 0) to support your Ruby & RubyGems was 2.4.22. Try installing it with `gem install bundler -v 2.4.22`
24.10 	bundler requires Ruby version >= 3.0.0. The current ruby version is 2.6.10.210.
------
failed to solve: process "/bin/sh -c gem install bundler" did not complete successfully: exit code: 1

or

 => ERROR [app base 4/4] RUN gem install bundler -v 2.4.22 && bundle update --bundler && bundle install                                                                                                                                           11.9s
------
 > [app base 4/4] RUN gem install bundler -v 2.4.22 && bundle update --bundler && bundle install:
11.75 Successfully installed bundler-2.4.22
11.75 1 gem installed
11.83 Your RubyGems version (3.0.3.1) has a bug that prevents `required_ruby_version` from working for Bundler. Any scripts that use `gem install bundler` will break as soon as Bundler drops support for your Ruby version. Please upgrade RubyGems to avoid future breakage and silence this warning by running `gem update --system 3.2.3`
11.85 Could not locate Gemfile
------
failed to solve: process "/bin/sh -c gem install bundler -v 2.4.22 && bundle update --bundler && bundle install" did not complete successfully: exit code: 10

It's because gem (RubyGems) and bundler need to be installed/updated as follows:

# STAGE 1: Initial setup
FROM ruby:2.6 as base
WORKDIR /app

# Install missing package(s)
RUN apt-get update -qq && apt-get install -yq --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Prep for gem installation
# NOTE: The RubyGems version that comes with Ruby 2.6 contains a bug and must be updated to v3.2.3.
#       Also, Bundler must be pinned to v2.4.22 because it is the last version that works with Ruby 2.6.
#       When Ruby gets upgraded, the pinned versions can get removed below.
RUN gem update --system 3.2.3 && gem install bundler -v 2.4.22

# STAGE 2: Install development & runtime gems
# NOTE: Copy *only* the files you absolutely need for this stage, otherwise caching will become useless
FROM base as gems
COPY Gemfile Gemfile.lock ../*.gemspec ./
COPY lib/path/to/version.rb lib/path/to/version.rb
RUN bundle update --bundler && bundle install

# STAGE 3: Install your gem & copy over cached gems
FROM base as final
COPY --from=gems /usr/local/bundle /usr/local/bundle
COPY .. /app

# Install the gem CLI
RUN bundle exec rake install
# Alternatively, you can run:
# RUN gem build *.gemspec && gem install *.gem

# Keep container running
CMD ["tail", "-f", "/dev/null"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment