Adding, removing, or updating gems currently have bad cache misses for the default docker image produced with rails new . This particularly affects rails apps with many native dependencies that must be rebuilt. By using a docker mount cache the build process can reuse the work that was previously done to fetch and install dependencies.
On a freshly created rails app using postgres, adding sidekiq (after initially building the docker image without the sidekiq gem), a mount cache halves the build time.
# Without a mount cache adding sidekiq
> bundle add sidekiq && time docker build .
docker build . 0.23s user 0.21s system 1% cpu 35.999 total
# With a mount cache adding sidekiq
> bundle add sidekiq && time docker build .
docker build . 0.17s user 0.13s system 1% cpu 18.277 total
The downside to this change is the Dockerfile becomes more complicated
RUN --mount=type=cache,target=/tmp/bundle-cache \
cp -r /tmp/bundle-cache/* "${BUNDLE_PATH}/" 2>/dev/null || true && \
bundle install && \
cp -r "${BUNDLE_PATH}"/* /tmp/bundle-cache/ && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
# -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
bundle exec bootsnap precompile -j 1 --gemfile
vs
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
# -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
bundle exec bootsnap precompile -j 1 --gemfile
Is there any appetite for a pull request here?