Skip to content

Commit 78061eb

Browse files
committed
speedup docker build time
1 parent ec684bb commit 78061eb

File tree

5 files changed

+32
-44
lines changed

5 files changed

+32
-44
lines changed

railties/lib/rails/generators/app_base.rb

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -561,18 +561,29 @@ def dockerfile_binfile_fixups
561561
binfixups
562562
end
563563

564+
def dockerfile_base_packages
565+
# Add curl to work with the default healthcheck strategy in Kamal
566+
packages = ["curl"]
567+
568+
# ActiveRecord databases
569+
packages << base_package_for_database unless skip_active_record?
570+
571+
# ActiveStorage preview support
572+
packages << "libvips" unless skip_active_storage?
573+
574+
# jemalloc for memory optimization
575+
packages << "libjemalloc2"
576+
577+
packages.compact.sort
578+
end
579+
564580
def dockerfile_build_packages
565581
# start with the essentials
566582
packages = %w(build-essential git pkg-config)
567583

568584
# add database support
569585
packages << build_package_for_database unless skip_active_record?
570586

571-
# ActiveStorage preview support
572-
packages << "libvips" unless skip_active_storage?
573-
574-
packages << "curl" if using_js_runtime?
575-
576587
packages << "unzip" if using_bun?
577588

578589
# node support, including support for building native modules
@@ -585,22 +596,6 @@ def dockerfile_build_packages
585596
packages.compact.sort
586597
end
587598

588-
def dockerfile_deploy_packages
589-
# Add curl to work with the default healthcheck strategy in Kamal
590-
packages = ["curl"]
591-
592-
# ActiveRecord databases
593-
packages << deploy_package_for_database unless skip_active_record?
594-
595-
# ActiveStorage preview support
596-
packages << "libvips" unless skip_active_storage?
597-
598-
# jemalloc for memory optimization
599-
packages << "libjemalloc2"
600-
601-
packages.compact.sort
602-
end
603-
604599
def css_gemfile_entry
605600
return if options[:api]
606601
return unless options[:css]

railties/lib/rails/generators/database.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def build_package_for_database(database = options[:database])
6767
end
6868
end
6969

70-
def deploy_package_for_database(database = options[:database])
70+
def base_package_for_database(database = options[:database])
7171
case database
7272
when "mysql" then "default-mysql-client"
7373
when "postgresql" then "postgresql-client"

railties/lib/rails/generators/rails/app/templates/Dockerfile.tt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
77
# Rails app lives here
88
WORKDIR /rails
99

10+
# Install base packages
11+
RUN apt-get update -qq && \
12+
apt-get install --no-install-recommends -y <%= dockerfile_base_packages.join(" ") %>
13+
1014
# Set production environment
1115
ENV RAILS_ENV="production" \
1216
BUNDLE_DEPLOYMENT="1" \
1317
BUNDLE_PATH="/usr/local/bundle" \
1418
BUNDLE_WITHOUT="development"
1519

16-
1720
# Throw-away build stage to reduce size of final image
1821
FROM base as build
1922

2023
# Install packages needed to build gems<%= using_node? ? " and node modules" : "" %>
21-
RUN apt-get update -qq && \
22-
apt-get install --no-install-recommends -y <%= dockerfile_build_packages.join(" ") %>
24+
RUN apt-get install --no-install-recommends -y <%= dockerfile_build_packages.join(" ") %>
2325

2426
<% if using_node? -%>
2527
# Install JavaScript dependencies
@@ -79,13 +81,9 @@ RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
7981
# Final stage for app image
8082
FROM base
8183

82-
<% unless dockerfile_deploy_packages.empty? -%>
83-
# Install packages needed for deployment
84-
RUN apt-get update -qq && \
85-
apt-get install --no-install-recommends -y <%= dockerfile_deploy_packages.join(" ") %> && \
86-
rm -rf /var/lib/apt/lists /var/cache/apt/archives
84+
# Clean up installation packages to reduce image size
85+
RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives
8786

88-
<% end -%>
8987
# Copy built artifacts: gems, application
9088
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
9189
COPY --from=build /rails /rails

railties/lib/rails/generators/rails/app/templates/github/ci.yml.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ jobs:
9999
<%- end -%>
100100
steps:
101101
- name: Install packages
102-
run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable <%= (dockerfile_deploy_packages + [build_package_for_database]).join(" ") %>
102+
run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable <%= (dockerfile_base_packages + [build_package_for_database]).join(" ") %>
103103

104104
- name: Checkout code
105105
uses: actions/checkout@v4

railties/test/fixtures/Dockerfile.test

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,28 @@ FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
77
# Rails app lives here
88
WORKDIR /rails
99

10+
# Install base packages
11+
RUN apt-get update -qq && \
12+
apt-get install --no-install-recommends -y curl libsqlite3-0 libvips
13+
1014
# Set production environment
1115
ENV RAILS_ENV="production" \
1216
BUNDLE_DEPLOYMENT="1" \
1317
BUNDLE_PATH="/usr/local/bundle" \
1418
BUNDLE_WITHOUT="development"
1519

16-
1720
# Throw-away build stage to reduce size of final image
1821
FROM base as build
1922

2023
# Install packages needed to build gems
21-
RUN apt-get update -qq && \
22-
apt-get install --no-install-recommends -y build-essential git libvips pkg-config
23-
24-
24+
RUN apt-get install --no-install-recommends -y build-essential git pkg-config
2525

2626
# Install application gems
2727
COPY Gemfile Gemfile.lock ./
2828
RUN bundle install && \
2929
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
3030
bundle exec bootsnap precompile --gemfile
3131

32-
33-
3432
# Copy application code
3533
COPY . .
3634

@@ -40,14 +38,11 @@ RUN bundle exec bootsnap precompile app/ lib/
4038
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
4139
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
4240

43-
4441
# Final stage for app image
4542
FROM base
4643

47-
# Install packages needed for deployment
48-
RUN apt-get update -qq && \
49-
apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \
50-
rm -rf /var/lib/apt/lists /var/cache/apt/archives
44+
# Clean up installation packages to reduce image size
45+
RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives
5146

5247
# Copy built artifacts: gems, application
5348
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"

0 commit comments

Comments
 (0)