Docker Rails Fix Bundler 2 problem

Pagorn Phusaisakul
2 min readSep 20, 2020
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

$ docker-compose up rails 

Recreating rails ... done
Attaching to rails
rails | Bundler::LockfileError: You must use Bundler 2 or greater with this lockfile.
...
ld-backend | bundler: failed to load command: rails (/usr/local/bundle/bin/rails)
ld-backend exited with code 1

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
BUNDLED WITH
2.1.4

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

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

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

For docker-compose,

$ docker-compose build --no-cache rails
$ docker-compose up rails

For docker,

$ docker build --no-cache -t rails .
$ docker run rails

4. That’s it! Enjoys 🎉

--

--