فهرست منبع

Clean dev docker build stage

Andrew Swistak 6 سال پیش
والد
کامیت
fabbb45a3c
1فایلهای تغییر یافته به همراه17 افزوده شده و 22 حذف شده
  1. 17 22
      Dockerfile

+ 17 - 22
Dockerfile

@@ -4,11 +4,8 @@ ARG BUILD_PACKAGES="build-base git"
 ARG DEV_PACKAGES="postgresql-dev yarn"
 ARG RUBY_PACKAGES="tzdata"
 
-# Add git to clone Rspec repositories and build-base to compile nokogiri gem
-# Additionally, we need to set timezone information
 RUN apk update && apk add --no-cache $BUILD_PACKAGES $DEV_PACKAGES $RUBY_PACKAGES
 
-# Make an app directory, just like Heroku.
 RUN mkdir /app
 WORKDIR /app
 
@@ -18,49 +15,47 @@ FROM base AS builder
 # Copying the Gemfile separately allows the image to be cached.
 # These steps are not rerun unless the Gemfile or Gemfile.lock is changed.
 COPY Gemfile Gemfile.lock /app/
-RUN bundle install --jobs 4 --retry 5 --without development test
+RUN bundle install --jobs 4 --retry 5
 
 # Again, copy seperately so we can cache this step.
 COPY package.json yarn.lock /app/
-RUN yarn install
+RUN yarn install && yarn cache clean
 
 # Copy our app over now
 COPY . /app
 
-# And compile our assets, removing unneeded node_modules directory afterwards
-RUN rails assets:precompile && \
-      rm -rf node_modules tmp/cache app/assets log spec
-
-# Copy the docker configurations over
+# Copy the docker configuration to the correct location
 COPY config/database.yml.docker config/database.yml
 
+# Compile our assets, moving unneeded directories out of way for the prod image
+# after compilation has completed.
+RUN rails assets:precompile && \
+      rm -rf tmp/cache log && \
+      mkdir -p /var/cache/app/app && \
+      mv node_modules spec /var/cache/app && \
+      mv app/assets /var/cache/app/app
+
 ################################################################################
 FROM base AS dev
 
 ENV RAILS_ENV=development
 
-# We want to include all of our dependencies, so grab what we have already
 COPY --from=builder /usr/local/bundle/ /usr/local/bundle/
 COPY --from=builder /app/ /app/
-
-# Re-add items removed from the builder
-COPY app/assets/ /app/app/assets/
-COPY spec/ /app/spec/
-
-# (Re)install the missing development dependencies
-RUN bundle install --jobs 4 --retry 5 --with development test && \
-      yarn install
+COPY --from=builder /var/cache/app/ /app/
 
 ################################################################################
 # We want a tiny image, so don't start from base
 FROM ruby:2.6.3-alpine AS prod
 
 ENV RAILS_ENV=production
-
 RUN apk add --update --no-cache $RUBY_PACKAGES
 
-# Grab everything from the builder
-COPY --from=builder /usr/local/bundle/ /usr/local/bundle/
+# (Re)install only dependencies we need for prod instead of copying everything
+# from the builder
+RUN bundle install --jobs 4 --retry 5 --without development test
+
+# Grab the app with all compiled assets and docker configuration from builder
 COPY --from=builder /app/ /app/
 
 # Run the server