DEV Community

Zack Kollar
Zack Kollar

Posted on • Edited on

How to add a specific version of bundler to your Ruby on Rails Dockerfile

When working on a slightly older Ruby/RoR project, I updated some dependencies (specifically from DependaBot pull requests) and found out that my bundler version was causing some interesting errors. Mostly incompatibility with my Gemfile.lock and what version it expected for certain deps.

The error message was instructing me to update my bundler version locally.

This however can work for local projects, but when Docker is involved this isn't quite as obvious for us non-monk Ruby devs.

Here's your solution:

ENV BUNDLER_VERSION='X.X.X'
RUN gem install bundler --no-document -v 'X.X.X'
Enter fullscreen mode Exit fullscreen mode

Replace X.X.X with whatever version you require, for me it was 2.2.4. I placed this after my system package installs to possibly help with image build caching. (This could be entirely wrong as well!)

This doesn't just apply to legacy projects or unsupported Docker image bases, this can be super helpful for managing the exact bundler version you require in your toolset.

Here's the final Dockerfile for my project for reference. I'm using webpacker for isolated React components and PostCSS magic.

FROM ruby:2.5.1

# Replace with whoever you are!
LABEL maintainer="YOU@YOU.YOU"

# Allow apt to work with https-based sources
RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
  apt-transport-https

# Ensure we install an up-to-date version of Node
# see https://github.com/yarnpkg/yarn/issues/2888
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -

# Ensure latest packages for Yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | \
  tee /etc/apt/sources.list.d/yarn.list

# Convention to use update and install on same line to actually install new packages
# We also use the line separation to make this easier to change
RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
  nodejs \
  yarn

# Setup our bundler version specifically
ENV BUNDLER_VERSION='X.X.X'
RUN gem install bundler --no-document -v 'X.X.X'

# Load our Gemfile and JS locks
COPY Gemfile* $YOUR_APPLICATION_PATH
COPY package.json $YOUR_APPLICATION_PATH

WORKDIR $YOUR_APPLICATION_PATH

# Install our dependencies
ENV BUNDLE_PATH /gems
RUN bundle install

# Handle yarn caching and install deps
COPY package.json yarn.lock ./
RUN yarn install --check-files

# Build the static assets and webpack data
COPY . $YOUR_APPLICATION_PATH
RUN bundle exec rake assets:precompile 

# Serve the application
ENV RAILS_SERVE_STATIC_FILES=true
ENV RAILS_LOG_TO_STDOUT=true
CMD ["bin/rails", "s", "-b", "0.0.0.0", "-e", "production"]

Enter fullscreen mode Exit fullscreen mode

I hope somebody struggling with any of these problems finds this! Please feel free to contact me directly if this doesn't work. Also if you're not using webpacker/shakapacker(2022).

Good luck and happy hacking!

Top comments (1)

Collapse
 
seedyrom profile image
Zack Kollar

Remember to replace X.X.X with whatever version you're expecting! It's a placeholder value!!!