Docker Rails Fix Bundler 2 problem

Image by Michal Jarmoluk from Pixabay

In Ruby on Rails, we have Bundler to handle gem. You might be stuck with an error message You must use Bundler 2 or greater with this lockfile. when our Gemfile.lock is using Bundler version 2

For this example, we will use Bundler version 2.1.4 and Ruby version 2.3.1

  1. Find your bundler version at Gemfile.lock

2. Add commandsENV BUNDLER_VERSION=BUNDLER_VERSION and RUN gem install bundler:BUNDLER_VERSION before RUN bundle install command in Dockerfile like this

RUN apt-get update -qq && apt-get install -y apt-utils nodejs postgresql-client# Copy project to /rails
RUN mkdir /rails
WORKDIR /rails
COPY Gemfile* ./
RUN gem install bundler:2.1.4
RUN bundle installCOPY . .RUN rm -f /rails/tmp/pids/server.pidEXPOSE 3000CMD rails server -b

3. Try to build rails without cache then start it again

For docker-compose,

For docker,

4. That’s it! Enjoys 🎉