Skip to content

Commit

Permalink
assets:precompile in the same layer after installing dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Vignesh.N committed Aug 2, 2021
1 parent 629df6f commit 0abf8f9
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions Dockerfile
@@ -1,39 +1,16 @@
# To use or update to a ruby version, change {BASE_RUBY_IMAGE}
## To use or update to a ruby version, change {BASE_RUBY_IMAGE}
ARG BASE_RUBY_IMAGE=ruby:2.7.4-alpine3.12
# BASE_RUBY_IMAGE_WITH_GEMS_AND_NODE_MODULES will default to apply-for-teacher-training-gems-node-modules
# building all layers above it if a value is not specidied during the build
ARG BASE_RUBY_IMAGE_WITH_GEMS_AND_NODE_MODULES=apply-for-teacher-training-gems-node-modules

# Stage 1: install-gems-node-modules, download gems and node modules.
FROM ${BASE_RUBY_IMAGE} AS install-gems-node-modules

ARG BUILD_DEPS="git gcc libc-dev make nodejs yarn postgresql-dev build-base libxml2-dev libxslt-dev ttf-ubuntu-font-family"
ENV WKHTMLTOPDF_GEM=wkhtmltopdf-binary-edge-alpine

WORKDIR /app
# Stage 1: gems-node-modules, build gems and node modules.
FROM ${BASE_RUBY_IMAGE} AS gems-node-modules

COPY Gemfile Gemfile.lock package.json yarn.lock ./
ENV BUILD_DEPS="git gcc libc-dev make postgresql-dev build-base libxml2-dev libxslt-dev ttf-ubuntu-font-family"

RUN apk -U upgrade && \
apk add --update --no-cache --virtual .gem-installdeps $BUILD_DEPS && \
gem update --system && \
find / -wholename '*default/bundler-*.gemspec' -delete && \
rm -rf /usr/local/bin/bundle && \
gem install bundler -v 2.1.4 && \
bundler -v && \
bundle config set no-cache 'true' && \
bundle config set no-binstubs 'true' && \
bundle --retry=5 --jobs=4 --without=development --with=production && \
yarn install --check-files && \
apk del .gem-installdeps && \
rm -rf /usr/local/bundle/cache && \
find /usr/local/bundle/gems -name "*.c" -delete && \
find /usr/local/bundle/gems -name "*.h" -delete && \
find /usr/local/bundle/gems -name "*.o" -delete

# Stage 2: apply-for-teacher-training-gems-node-modules, reduce size of gems-node-modules and only keep required files.
# published as dfedigital/apply-for-teacher-training-gems-node-modules
FROM ${BASE_RUBY_IMAGE} AS gems-node-modules
apk add --update --no-cache nodejs yarn tzdata libpq libxml2 libxslt graphviz && \
echo "Europe/London" > /etc/timezone && \
cp /usr/share/zoneinfo/Europe/London /etc/localtime

ENV WKHTMLTOPDF_GEM=wkhtmltopdf-binary-edge-alpine \
RAILS_ENV=production \
Expand All @@ -43,21 +20,28 @@ ENV WKHTMLTOPDF_GEM=wkhtmltopdf-binary-edge-alpine \
BLAZER_DATABASE_URL=testURL \
GOVUK_NOTIFY_CALLBACK_API_KEY=TestKey

RUN apk -U upgrade && \
apk add --update --no-cache nodejs yarn tzdata libpq libxml2 libxslt graphviz && \
echo "Europe/London" > /etc/timezone && \
cp /usr/share/zoneinfo/Europe/London /etc/localtime
WORKDIR /app

COPY --from=install-gems-node-modules /app /app
COPY --from=install-gems-node-modules /usr/local/bundle/ /usr/local/bundle/
COPY Gemfile Gemfile.lock ./

RUN gem update --system && \
bundler -v && \
bundle config set no-cache 'true' && \
bundle config set no-binstubs 'true' && \
bundle --retry=5 --jobs=4 --without=development --with=production \
apk del .gem-installdeps && \
rm -rf /usr/local/bundle/cache

COPY package.json yarn.lock ./

RUN yarn install --check-files

WORKDIR /app
COPY . .

RUN bundle exec rake assets:precompile && \
rm -rf tmp/* log/* node_modules /tmp/*

# Stage 3: production, copy application code and compiled assets to base ruby image.
# Stage 2: production, copy application code and compiled assets to base ruby image.
# Depends on assets-precompile stage which can be cached from a pre-built image
# by specifying a fully qualified image name or will default to packages-prod thereby rebuilding all 3 stages above.
# If a existing base image name is specified Stage 1 & 2 will not be built and gems and dev packages will be used from the supplied image.
Expand Down

0 comments on commit 0abf8f9

Please sign in to comment.