Optimizing Dockerfile caching for Bundler

In order for the Docker build cache to cache gems installed via Bundler, it’s necessary to add the Gemfile and Gemfile.lock files to the image, and run bundle install, before adding the rest of the repo (via ADD .). Here’s an example of how that might look in a Dockerfile:

FROM ruby

# If needed, install system dependencies here

# Add Gemfile and Gemfile.lock first for caching
ADD Gemfile /app/
ADD Gemfile.lock /app/
WORKDIR /app
RUN bundle install

ADD . /app

# If needed, add additional RUN commands here