Skip to content

Commit be287ac

Browse files
authored
Merge pull request rails#47915 from javierav/feature/dockerfile-skip-options
Improve Dockerfile generation when use some skip options
2 parents 965f6d4 + 08cb031 commit be287ac

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

railties/lib/rails/generators/app_base.rb

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def web_server_gemfile_entry # :doc:
271271
end
272272

273273
def asset_pipeline_gemfile_entry
274-
return if options[:skip_asset_pipeline]
274+
return if skip_asset_pipeline?
275275

276276
if options[:asset_pipeline] == "sprockets"
277277
GemfileEntry.floats "sprockets-rails",
@@ -335,7 +335,11 @@ def keeps? # :doc:
335335
end
336336

337337
def sqlite3? # :doc:
338-
!options[:skip_active_record] && options[:database] == "sqlite3"
338+
!skip_active_record? && options[:database] == "sqlite3"
339+
end
340+
341+
def skip_active_record? # :doc:
342+
options[:skip_active_record]
339343
end
340344

341345
def skip_active_storage? # :doc:
@@ -354,12 +358,16 @@ def skip_action_text? # :doc:
354358
options[:skip_action_text]
355359
end
356360

361+
def skip_asset_pipeline? # :doc:
362+
options[:skip_asset_pipeline]
363+
end
364+
357365
def skip_sprockets?
358-
options[:skip_asset_pipeline] || options[:asset_pipeline] != "sprockets"
366+
skip_asset_pipeline? || options[:asset_pipeline] != "sprockets"
359367
end
360368

361369
def skip_propshaft?
362-
options[:skip_asset_pipeline] || options[:asset_pipeline] != "propshaft"
370+
skip_asset_pipeline? || options[:asset_pipeline] != "propshaft"
363371
end
364372

365373

@@ -511,10 +519,10 @@ def dockerfile_binfile_fixups
511519

512520
def dockerfile_build_packages
513521
# start with the essentials
514-
packages = %w(build-essential git)
522+
packages = %w(build-essential git pkg-config)
515523

516-
# add databases: sqlite3, postgres, mysql
517-
packages += %w(pkg-config libpq-dev default-libmysqlclient-dev)
524+
# add database support
525+
packages << build_package_for_database unless skip_active_record?
518526

519527
# ActiveStorage preview support
520528
packages << "libvips" unless skip_active_storage?
@@ -543,17 +551,19 @@ def dockerfile_build_packages
543551
end
544552
end
545553

546-
packages.sort
554+
packages.compact.sort
547555
end
548556

549557
def dockerfile_deploy_packages
550-
# start with databases: sqlite3, postgres, mysql
551-
packages = %w(libsqlite3-0 postgresql-client default-mysql-client)
558+
packages = []
559+
560+
# ActiveRecord databases
561+
packages << deploy_package_for_database unless skip_active_record?
552562

553563
# ActiveStorage preview support
554564
packages << "libvips" unless skip_active_storage?
555565

556-
packages.sort
566+
packages.compact.sort
557567
end
558568

559569
# CSS processors other than Tailwind and Sass require a node-based JavaScript environment. So overwrite the normal JS default
@@ -727,6 +737,15 @@ def git_init_command
727737
def edge_branch
728738
self.class.edge_branch
729739
end
740+
741+
def dockerfile_chown_directories
742+
directories = %w(log tmp)
743+
744+
directories << "storage" unless skip_active_storage? && !sqlite3?
745+
directories << "db" unless skip_active_record?
746+
747+
directories.sort
748+
end
730749
end
731750
end
732751
end

railties/lib/rails/generators/database.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ def convert_database_option_for_jruby
3838
end
3939
end
4040

41+
def build_package_for_database(database = options[:database])
42+
case database
43+
when "mysql" then "default-libmysqlclient-dev"
44+
when "postgresql" then "libpq-dev"
45+
else nil
46+
end
47+
end
48+
49+
def deploy_package_for_database(database = options[:database])
50+
case database
51+
when "mysql" then "default-mysql-client"
52+
when "postgresql" then "postgresql-client"
53+
when "sqlite3" then "libsqlite3-0"
54+
else nil
55+
end
56+
end
57+
4158
private
4259
def mysql_socket
4360
@mysql_socket ||= [

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ RUN bundle exec bootsnap precompile app/ lib/
5858
<%= "RUN " + dockerfile_binfile_fixups.join(" && \\\n ") %>
5959

6060
<% end -%>
61-
<% unless options.api? -%>
61+
<% unless options.api? || skip_asset_pipeline? -%>
6262
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
6363
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
6464

@@ -67,18 +67,20 @@ RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
6767
# Final stage for app image
6868
FROM base
6969

70+
<% unless dockerfile_deploy_packages.empty? -%>
7071
# Install packages needed for deployment
7172
RUN apt-get update -qq && \
7273
apt-get install --no-install-recommends -y <%= dockerfile_deploy_packages.join(" ") %> && \
7374
rm -rf /var/lib/apt/lists /var/cache/apt/archives
7475

76+
<% end -%>
7577
# Copy built artifacts: gems, application
7678
COPY --from=build /usr/local/bundle /usr/local/bundle
7779
COPY --from=build /rails /rails
7880

7981
# Run and own only the runtime files as a non-root user for security
8082
RUN useradd rails --home /rails --shell /bin/bash && \
81-
chown -R rails:rails db log storage tmp
83+
chown -R rails:rails <%= dockerfile_chown_directories.join(" ") %>
8284
USER rails:rails
8385

8486
# Entrypoint prepares the database.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/bin/bash -e
22

3+
<% unless skip_active_record? -%>
34
# If running the rails server then create or migrate existing database
45
if [ "${*}" == "./bin/rails server" ]; then
56
./bin/rails db:prepare
67
fi
78

9+
<% end -%>
810
exec "${@}"

0 commit comments

Comments
 (0)