lib/generators/templates/Dockerfile.erb in dockerfile-rails-0.0.1 vs lib/generators/templates/Dockerfile.erb in dockerfile-rails-0.0.2
- old
+ new
@@ -8,37 +8,84 @@
WORKDIR /rails
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_PATH="vendor/bundle" \
- BUNDLE_WITHOUT="development:test"
+ BUNDLE_WITHOUT="<%= options.ci? ? 'test' : 'development:test' %>"
+ARG BUNDLER_VERSION=<%= Bundler::VERSION %>
+RUN gem update --system --no-document && \
+ gem install -N bundler -v ${BUNDLER_VERSION}
-# Throw-away build stage to reduce size of final image
-FROM base as build
+# Throw-away build stage<%= parallel? ? 's' : '' %> to reduce size of final image
+FROM base as <%= parallel? ? 'pre' : '' %>build
+
# Install packages need to build gems<%= using_node? ? " and node modules" : "" %>
+<% if options.cache? %>
+RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
+ --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
+ apt-get update -qq && \
+<% else -%>
RUN apt-get update -qq && \
+<% end -%>
apt-get install -y <%= build_packages.join(" ") %>
+<% if parallel? -%>
+
+FROM prebuild as node
+
+<% end -%>
<% if using_node? -%>
# Install JavaScript dependencies
ARG NODE_VERSION=<%= node_version %>
ARG YARN_VERSION=<%= yarn_version %>
ENV VOLTA_HOME="/usr/local"
RUN curl https://get.volta.sh | bash && \
volta install node@$NODE_VERSION yarn@$YARN_VERSION
<% end -%>
+<% if parallel? -%>
+# Install node modules
+COPY package.json yarn.lock ./
+<% if options.cache? -%>
+RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
+ YARN_CACHE_FOLDER=/root/.yarn yarn install
+<% else -%>
+RUN yarn install
+<% end -%>
+
+
+FROM prebuild as build
+
+<% end -%>
# Install application gems
COPY Gemfile Gemfile.lock ./
-RUN bundle install
+<% if options.cache? -%>
+RUN --mount=type=cache,id=bld-gem-cache,sharing=locked,target=/srv/vendor \
+ bundle config set app_config .bundle && \
+ bundle config set path /srv/vendor && \
+ bundle _${BUNDLER_VERSION}_ install && \
+ bundle clean && \
+ mkdir -p vendor && \
+ bundle config set path vendor && \
+ cp -ar /srv/vendor .
+<% else -%>
+RUN bundle _${BUNDLER_VERSION}_ install
+<% end -%>
-<% if using_node? -%>
+<% if parallel? -%>
+asdf
+<% elsif using_node? -%>
# Install node modules
COPY package.json yarn.lock ./
+<% if options.cache? -%>
+RUN --mount=type=cache,id=bld-yarn-cache,target=/root/.yarn \
+ YARN_CACHE_FOLDER=/root/.yarn yarn install
+<% else -%>
RUN yarn install
+<% end -%>
<% end -%>
# Copy application code
COPY . .
@@ -52,22 +99,34 @@
<%= "RUN " + binfile_fixups.join(" && \\\n ") %>
<% end -%>
<% unless api_only? -%>
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
-RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
+RUN SECRET_KEY_BASE<%= Rails::VERSION::MAJOR<7 || Rails::VERSION::STRING.start_with?('7.0') ? '=DUMMY' : '_DUMMY=1' %> ./bin/rails assets:precompile
<% end -%>
# Final stage for app image
FROM base
+<% unless deploy_packages.empty? -%>
# Install packages need for deployment
+<% if options.cache? -%>
+RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
+ --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
+ apt-get update -qq && \
+ apt-get install --no-install-recommends -y <%= deploy_packages.join(" ") %>
+<% else -%>
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y <%= deploy_packages.join(" ") %> && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
+<% end -%>
+<% end -%>
# Copy built application from second stage
+<% if options.ci? -%>
+COPY --from=build /usr/local/bundle /usr/local/bundle
+<% end -%>
COPY --from=build /rails /rails
# Deployment options
ENV RAILS_LOG_TO_STDOUT="1" \
RAILS_SERVE_STATIC_FILES="true"
\ No newline at end of file