Last active
August 11, 2024 21:13
-
-
Save dpaluy/fde0a6f0b1ed70edc8af3cffedac8a9e to your computer and use it in GitHub Desktop.
Optimized Dockerfile for building Ruby on Rails apps with YJIT, jemallocand bootsnap enabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create base image | |
FROM ruby:3.2-slim-bookworm AS base | |
# Set ENV variables | |
ENV RAILS_ENV=production \ | |
RACK_ENV=production \ | |
NODE_ENV=production \ | |
APP_ENV=production \ | |
RAILS_LOG_TO_STDOUT=true \ | |
RAILS_MAX_THREADS=10 \ | |
RAILS_SERVE_STATIC_FILES=true | |
ENV GEM_HOME="/usr/local/bundle" \ | |
BUNDLE_WITHOUT="development:test" \ | |
BUNDLE_FROZEN="1" \ | |
BUNDLE_JOBS="32" | |
ENV PATH $GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH | |
WORKDIR /app | |
RUN apt-get update -qq && \ | |
apt-get upgrade -y --no-install-recommends | |
RUN apt-get -y --no-install-recommends install zip gnupg tzdata curl wget libjemalloc2 libvips \ | |
apt-transport-https apt-utils ca-certificates postgresql-client redis-tools | |
# Update gems and bundler | |
RUN gem update --system --no-document | |
# Clean cache | |
RUN apt-get clean && rm -f /var/lib/apt/lists/*_*; | |
RUN rm -rf /root/.local | |
# Create setup image | |
FROM base AS setup | |
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ | |
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list | |
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash - | |
RUN apt-get update -qq | |
RUN apt-get install --no-install-recommends -y build-essential libpq-dev zlib1g-dev libssl-dev libreadline-dev \ | |
libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev libcurl4-openssl-dev libffi-dev pkg-config dirmngr \ | |
git-core nodejs yarn libvips-dev python-is-python3 | |
# Install application gems | |
COPY Gemfile* ./ | |
RUN gem install bundler --no-document | |
RUN bundle install | |
# Install JS libs | |
COPY package.json *yarn* ./ | |
RUN yarn install --frozen-lockfile | |
# Copy application files | |
COPY . . | |
# Build assets | |
RUN ./bin/rails assets:precompile | |
# Remove extra files | |
RUN rm -rf node_modules | |
RUN rm -rf /usr/local/bundle/cache/* | |
# Create runtime image | |
FROM base AS runtime | |
ENV LD_PRELOAD="libjemalloc.so.2" \ | |
MALLOC_CONF="background_thread:true,metadata_thp:auto,dirty_decay_ms:5000,muzzy_decay_ms:5000,narenas:2" \ | |
RUBY_YJIT_ENABLE="1" | |
# Copy built artifacts: gems, application | |
COPY --from=setup /usr/local/bundle /usr/local/bundle | |
COPY --from=setup /app /app | |
# Precompile bootsnap code for faster boot times | |
RUN rm -rf /app/tmp/cache | |
RUN bundle exec bootsnap precompile --gemfile app/ lib/ | |
# Initialize entrypoint | |
EXPOSE 3000 | |
CMD ["./bin/rails", "server", "-p", "3000"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source: https://mailsnag.com/blog/optimized-ruby-dockerfile/