Переглянути джерело

Aggressively remove objects from the builder image

Andrew Swistak 6 роки тому
батько
коміт
89604a3bb9
1 змінених файлів з 19 додано та 15 видалено
  1. 19 15
      Dockerfile

+ 19 - 15
Dockerfile

@@ -1,15 +1,20 @@
-FROM ruby:2.6.3-alpine as builder
+FROM ruby:2.6.3-alpine AS base
+
+ARG BUILD_PACKAGES="build-base git"
+ARG DEV_PACKAGES="postgresql-dev nodejs 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 add --update git build-base postgresql-dev yarn tzdata && \
-      cp /usr/share/zoneinfo/UTC /etc/localtime && \
-      echo "UTC" > /etc/timezone
+RUN apk add --update --no-cache $BUILD_PACKAGES $DEV_PACKAGES $RUBY_PACKAGES
 
 # Make an app directory, just like Heroku.
 RUN mkdir /app
 WORKDIR /app
 
+################################################################################
+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/
@@ -22,21 +27,20 @@ RUN yarn install
 # 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 vendor/assets spec
+
 # Copy the docker configurations over
 COPY config/database.yml.docker config/database.yml
 
-# And compile our assets
-RUN rails assets:precompile
-
-FROM ruby:2.6.3-alpine
+################################################################################
+# We want a tiny image, so don't start from base
+FROM ruby:2.6.3-alpine AS prod
 
-RUN apk add --update postgresql-dev tzdata && \
-      cp /usr/share/zoneinfo/UTC /etc/localtime && \
-      echo "UTC" > /etc/timezone
+ENV RAILS_ENV=production
 
-# Make an app directory, just like Heroku.
-RUN mkdir /app
-WORKDIR /app
+RUN apk add --update --no-cache $RUBY_PACKAGES
 
 # Grab everything from the builder
 COPY --from=builder /usr/local/bundle/ /usr/local/bundle/
@@ -44,4 +48,4 @@ COPY --from=builder /app/ /app/
 
 # Run the server
 EXPOSE 3000
-CMD rails server -p 3000
+CMD ["rails", "server", "-b", "0.0.0.0"]