Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Dockerfile installs commonly needed packages #46953

Merged
merged 1 commit into from Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions railties/lib/rails/generators/app_base.rb
Expand Up @@ -457,6 +457,46 @@ def dockerfile_binfile_fixups
binfixups
end

def dockerfile_packages
# start with the essentials
packages = %w(build-essential git)

# add databases: sqlite3, postgres, mysql
packages += %w(pkg-config libpq-dev default-libmysqlclient-dev)

# add redis in case Action Cable, caching, or sidekiq are added later
packages << "redis"

# ActiveStorage preview support
packages << "libvips" unless skip_active_storage?

# node support, including support for building native modules
if using_node?
packages += %w(curl node-gyp) # pkg-config already listed above

# module build process depends on Python, and debian changed
# how python is installed with the bullseye release. Below
# is based on debian release included with the Ruby images on
# Dockerhub.
case Gem.ruby_version
when /^2.7/
bullseye = ruby_version >= "2.7.4"
when /^3.0/
bullseye = ruby_version >= "3.0.2"
else
bullseye = true
end

if bullseye
packages << "python-is-python3"
else
packages << "python"
end
end

packages.sort
end

# CSS processors other than Tailwind require a node-based JavaScript environment. So overwrite the normal JS default
# if one such processor has been specified.
def adjusted_javascript_option
Expand Down
20 changes: 9 additions & 11 deletions railties/lib/rails/generators/rails/app/templates/Dockerfile.tt
@@ -1,24 +1,22 @@
# Make sure it matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=<%= Gem.ruby_version %>
FROM ruby:$RUBY_VERSION
FROM ruby:$RUBY_VERSION-slim

# Install packages need to build gems<%= using_node? ? " and node modules" : "" %>
RUN apt-get update -qq && \
apt-get install -y <%= dockerfile_packages.join(" ") %> && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man

<% if using_node? -%>
# Install JavaScript dependencies
ARG NODE_VERSION=<%= dockerfile_node_version %>
ARG YARN_VERSION=<%= dockerfile_yarn_version %>
ENV VOLTA_HOME="/root/.volta" \
PATH="$VOLTA_HOME/bin:/usr/local/bin:$PATH"
PATH="/root/.volta/bin:$PATH"
RUN curl https://get.volta.sh | bash && \
volta install node@$NODE_VERSION yarn@$YARN_VERSION

<% end -%>
<% if !skip_active_storage? -%>
# Install libvips for Active Storage preview support
RUN apt-get update -qq && \
apt-get install -y build-essential libvips && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man

<% end -%>
# Rails app lives here
WORKDIR /rails
Expand Down Expand Up @@ -56,4 +54,4 @@ ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
CMD ["./bin/rails", "server"]
2 changes: 2 additions & 0 deletions railties/test/generators/app_generator_test.rb
Expand Up @@ -772,6 +772,7 @@ def test_webpack_option

assert_file "Dockerfile" do |content|
assert_match(/yarn/, content)
assert_match(/node-gyp/, content)
end

assert_file ".node-version" do |content|
Expand Down Expand Up @@ -1042,6 +1043,7 @@ def test_dockerfile
assert_match(/assets:precompile/, content)
assert_match(/libvips/, content)
assert_no_match(/yarn/, content)
assert_no_match(/node-gyp/, content)
end
end

Expand Down