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 12fbde7
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions Dockerfile
@@ -1,21 +1,28 @@
# 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
# Stage 1: gems-node-modules, build gems and node modules.
FROM ${BASE_RUBY_IMAGE} AS 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
ARG BUILD_DEPS="git gcc libc-dev make postgresql-dev build-base libxml2-dev libxslt-dev ttf-ubuntu-font-family"

ENV WKHTMLTOPDF_GEM=wkhtmltopdf-binary-edge-alpine \
RAILS_ENV=production \
GOVUK_NOTIFY_API_KEY=TestKey \
AUTHORISED_HOSTS=127.0.0.1 \
SECRET_KEY_BASE=TestKey \
BLAZER_DATABASE_URL=testURL \
GOVUK_NOTIFY_CALLBACK_API_KEY=TestKey

WORKDIR /app

COPY Gemfile Gemfile.lock package.json yarn.lock ./

RUN apk -U upgrade && \
apk add --update --no-cache --virtual .gem-installdeps $BUILD_DEPS && \
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 \
gem update --system && \
find / -wholename '*default/bundler-*.gemspec' -delete && \
rm -rf /usr/local/bin/bundle && \
Expand All @@ -31,33 +38,12 @@ RUN apk -U upgrade && \
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

ENV WKHTMLTOPDF_GEM=wkhtmltopdf-binary-edge-alpine \
RAILS_ENV=production \
GOVUK_NOTIFY_API_KEY=TestKey \
AUTHORISED_HOSTS=127.0.0.1 \
SECRET_KEY_BASE=TestKey \
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

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

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 12fbde7

Please sign in to comment.