Skip to main content

First-Class Feature Flags. Out of the box.

Say goodbye to context switching and hello to feature flags in your IDE.

Gem install sassc is always going to be slow. Your Docker builds don't have to be.

· 3 min read
Jeffrey Chupp

There's an open issue from March of 2020 titled sassc is very slow to compile and install. The issue has people pleading for help and asking for sassc-ruby to ship precompiled binaries (as nokogiri does). The place this hurts the most is building your Rails app with Docker where you can pay a 10+ minute install time every time you modify any part of your Gemfile.lock

Oof.

I have good news for those still stuck on sassc: Your Docker builds don't have to be slow.

Docker enthusiasts know that layer caching is a huge time saver. But modifying your Gemfile.lock breaks your bundle install layer and causes the previous cache to be unusable. Even though sassc has been at version 2.4.0 since June of 2020 and isn't likely to be updated, even a minor version bump on any other gem in your Gemfile means you're reinstalling sassc again.

Fortunately the fix is a trivial change in your Dockerfile: before your RUN bundle install command, add RUN gem install sassc:2.4.0.

The sassc install will be cached as its own Docker layer and then your subsequent bundle install will use the existing sassc from disk.

You can use this strategy for other rarely-changed gems with native extensions for more savings.

Altogether this looks like:

FROM ruby:3.2.1

# 1. Install gems that rarely change or are very slow
ENV RAILS_VERSION 6.1.7.2
RUN gem install rails --version "$RAILS_VERSION"

# pull sassc out and install early so we don't pay the price on each gem change
RUN gem install sassc:2.4.0
RUN gem install bundler


# 2. Install gems that change more frequently
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY Gemfile /usr/src/app/
COPY Gemfile.lock /usr/src/app/

# don't bother installing development and test gems
RUN bundle config set --local without 'development test'
RUN bundle install --jobs 4

# 3. Now Move the app code in, because this changes every build
COPY . /usr/src/app

RUN RAILS_ENV=production bundle exec rake assets:precompile

That's it!

For us gem install sassc:2.4.0 inside bundler has saved 8 minutes per build. Hopefully this can help you too.

Speaking of things that are slow, have you ever wanted to change an environment variable variable without restarting your server? Ever wanted to change the log level without pushing a new build? Or perhaps you're simply interested in a fabulous Rails Feature Flag solution that has everything you need, but doesn't charge by the seat? If so, check us out here at Prefab!

Like what you read? You might want to check out what we're building at Prefab. Feature flags, dynamic config, and dynamic log levels. Free trials and great pricing for all of it.
See our Feature Flags