speedup docker build time

This commit is contained in:
Achmad Chun Chun 2024-02-13 20:26:14 +07:00
parent ec684bbe51
commit 78061ebfe8
5 changed files with 32 additions and 44 deletions

View File

@ -561,6 +561,22 @@ module Rails
binfixups
end
def dockerfile_base_packages
# Add curl to work with the default healthcheck strategy in Kamal
packages = ["curl"]
# ActiveRecord databases
packages << base_package_for_database unless skip_active_record?
# ActiveStorage preview support
packages << "libvips" unless skip_active_storage?
# jemalloc for memory optimization
packages << "libjemalloc2"
packages.compact.sort
end
def dockerfile_build_packages
# start with the essentials
packages = %w(build-essential git pkg-config)
@ -568,11 +584,6 @@ module Rails
# add database support
packages << build_package_for_database unless skip_active_record?
# ActiveStorage preview support
packages << "libvips" unless skip_active_storage?
packages << "curl" if using_js_runtime?
packages << "unzip" if using_bun?
# node support, including support for building native modules
@ -585,22 +596,6 @@ module Rails
packages.compact.sort
end
def dockerfile_deploy_packages
# Add curl to work with the default healthcheck strategy in Kamal
packages = ["curl"]
# ActiveRecord databases
packages << deploy_package_for_database unless skip_active_record?
# ActiveStorage preview support
packages << "libvips" unless skip_active_storage?
# jemalloc for memory optimization
packages << "libjemalloc2"
packages.compact.sort
end
def css_gemfile_entry
return if options[:api]
return unless options[:css]

View File

@ -67,7 +67,7 @@ module Rails
end
end
def deploy_package_for_database(database = options[:database])
def base_package_for_database(database = options[:database])
case database
when "mysql" then "default-mysql-client"
when "postgresql" then "postgresql-client"

View File

@ -7,19 +7,21 @@ FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
# Rails app lives here
WORKDIR /rails
# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y <%= dockerfile_base_packages.join(" ") %>
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
# Throw-away build stage to reduce size of final image
FROM base as build
# Install packages needed to build gems<%= using_node? ? " and node modules" : "" %>
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y <%= dockerfile_build_packages.join(" ") %>
RUN apt-get install --no-install-recommends -y <%= dockerfile_build_packages.join(" ") %>
<% if using_node? -%>
# Install JavaScript dependencies
@ -79,13 +81,9 @@ RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
# Final stage for app image
FROM base
<% unless dockerfile_deploy_packages.empty? -%>
# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y <%= dockerfile_deploy_packages.join(" ") %> && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Clean up installation packages to reduce image size
RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives
<% end -%>
# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

View File

@ -99,7 +99,7 @@ jobs:
<%- end -%>
steps:
- name: Install packages
run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable <%= (dockerfile_deploy_packages + [build_package_for_database]).join(" ") %>
run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable <%= (dockerfile_base_packages + [build_package_for_database]).join(" ") %>
- name: Checkout code
uses: actions/checkout@v4

View File

@ -7,21 +7,21 @@ FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
# Rails app lives here
WORKDIR /rails
# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libsqlite3-0 libvips
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
# Throw-away build stage to reduce size of final image
FROM base as build
# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libvips pkg-config
RUN apt-get install --no-install-recommends -y build-essential git pkg-config
# Install application gems
COPY Gemfile Gemfile.lock ./
@ -29,8 +29,6 @@ RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile
# Copy application code
COPY . .
@ -40,14 +38,11 @@ RUN bundle exec bootsnap precompile app/ lib/
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
# Final stage for app image
FROM base
# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Clean up installation packages to reduce image size
RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives
# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"