@@ -17,6 +17,7 @@ WORKDIR /rails
1717# Install base packages
1818RUN apt-get update -qq && \
1919 apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
20+ ln -s /usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2 /usr/local/lib/libjemalloc.so && \
2021 rm -rf /var/lib/apt/lists /var/cache/apt/archives
2122
2223# Install JavaScript dependencies
@@ -26,11 +27,12 @@ RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz
2627 /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
2728 rm -rf /tmp/node-build-master
2829
29- # Set production environment
30+ # Set production environment variables and enable jemalloc for reduced memory usage and latency.
3031ENV RAILS_ENV="production" \
3132 BUNDLE_DEPLOYMENT="1" \
3233 BUNDLE_PATH="/usr/local/bundle" \
33- BUNDLE_WITHOUT="development:test"
34+ BUNDLE_WITHOUT="development:test" \
35+ LD_PRELOAD="/usr/local/lib/libjemalloc.so"
3436
3537# Throw-away build stage to reduce size of final image
3638FROM base AS build
@@ -45,7 +47,8 @@ COPY Gemfile Gemfile.lock vendor ./
4547
4648RUN bundle install && \
4749 rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
48- bundle exec bootsnap precompile --gemfile
50+ # -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
51+ bundle exec bootsnap precompile -j 1 --gemfile
4952
5053# Install node modules
5154COPY package.json package-lock.json ./
@@ -55,8 +58,9 @@ RUN npm ci && \
5558# Copy application code
5659COPY . .
5760
58- # Precompile bootsnap code for faster boot times
59- RUN bundle exec bootsnap precompile app/ lib/
61+ # Precompile bootsnap code for faster boot times.
62+ # -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
63+ RUN bundle exec bootsnap precompile -j 1 app/ lib/
6064
6165# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
6266RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile && \
@@ -71,7 +75,6 @@ FROM base
7175# Run and own only the runtime files as a non-root user for security
7276RUN groupadd --system --gid 1000 rails && \
7377 useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash
74-
7578USER 1000:1000
7679
7780# Copy built artifacts: gems, application
0 commit comments