From 9b3805fc80c4fc9ada4b1acedcd2fcaf4e06ebbc Mon Sep 17 00:00:00 2001 From: Joey Eamigh <55670930+JoeyEamigh@users.noreply.github.com> Date: Tue, 17 Sep 2024 18:53:29 -0400 Subject: [PATCH] optimize order of the dockerfiles to leverage layer caching --- Dockerfile | 25 ++++++++++------------- Dockerfile.dev | 55 +++++++++++++++++++++++++------------------------- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/Dockerfile b/Dockerfile index 224fcfcb..d809b2d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,11 @@ FROM ruby:3.3.4-alpine -ENV APP_PATH /var/app -ENV BUNDLE_VERSION 2.5.9 -ENV BUNDLE_PATH /usr/local/bundle/gems -ENV TMP_PATH /tmp/ -ENV RAILS_LOG_TO_STDOUT true -ENV RAILS_PORT 3000 - -# Copy entrypoint scripts and grant execution permissions -COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh -RUN chmod +x /usr/local/bin/dev-entrypoint.sh - -# Copy application files to workdir -COPY . $APP_PATH +ENV APP_PATH=/var/app +ENV BUNDLE_VERSION=2.5.9 +ENV BUNDLE_PATH=/usr/local/bundle/gems +ENV TMP_PATH=/tmp/ +ENV RAILS_LOG_TO_STDOUT=true +ENV RAILS_PORT=3000 # Install dependencies for application RUN apk -U add --no-cache \ @@ -39,7 +32,7 @@ RUN gem install bundler --version "$BUNDLE_VERSION" \ # Navigate to app directory WORKDIR $APP_PATH -COPY Gemfile Gemfile.lock ./ +COPY Gemfile Gemfile.lock vendor .ruby-version ./ # Install missing gems RUN bundle config set --local path 'vendor/bundle' \ @@ -47,6 +40,10 @@ RUN bundle config set --local path 'vendor/bundle' \ COPY . ./ +# Copy entrypoint scripts and grant execution permissions +COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh +RUN chmod +x /usr/local/bin/dev-entrypoint.sh + EXPOSE $RAILS_PORT ENTRYPOINT [ "bundle", "exec" ] diff --git a/Dockerfile.dev b/Dockerfile.dev index 35b1f6c6..8d668cf3 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,39 +1,38 @@ FROM ruby:3.3.4-alpine -ENV APP_PATH /var/app -ENV BUNDLE_VERSION 2.5.9 -ENV BUNDLE_PATH /usr/local/bundle/gems -ENV TMP_PATH /tmp/ -ENV RAILS_LOG_TO_STDOUT true -ENV RAILS_PORT 3000 +ENV APP_PATH=/var/app +ENV BUNDLE_VERSION=2.5.9 +ENV BUNDLE_PATH=/usr/local/bundle/gems +ENV TMP_PATH=/tmp/ +ENV RAILS_LOG_TO_STDOUT=true +ENV RAILS_PORT=3000 + +# install dependencies for application +RUN apk -U add --no-cache \ + build-base \ + git \ + postgresql-dev \ + postgresql-client \ + libxml2-dev \ + libxslt-dev \ + nodejs \ + yarn \ + imagemagick \ + tzdata \ + less \ + # gcompat for nokogiri on mac m1 + gcompat \ + && rm -rf /var/cache/apk/* \ + && mkdir -p $APP_PATH + +RUN gem install bundler --version "$BUNDLE_VERSION" \ + && rm -rf $GEM_HOME/cache/* # copy entrypoint scripts and grant execution permissions COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh COPY ./test-docker-entrypoint.sh /usr/local/bin/test-entrypoint.sh RUN chmod +x /usr/local/bin/dev-entrypoint.sh && chmod +x /usr/local/bin/test-entrypoint.sh -# install dependencies for application -RUN apk -U add --no-cache \ -build-base \ -git \ -postgresql-dev \ -postgresql-client \ -libxml2-dev \ -libxslt-dev \ -nodejs \ -yarn \ -imagemagick \ -tzdata \ -less \ -# gcompat for nokogiri on mac m1 -gcompat \ -&& rm -rf /var/cache/apk/* \ -&& mkdir -p $APP_PATH - - -RUN gem install bundler --version "$BUNDLE_VERSION" \ -&& rm -rf $GEM_HOME/cache/* - # navigate to app directory WORKDIR $APP_PATH