From 434d6c26569a15ee1cce583146840a54967da79c Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Tue, 31 Dec 2019 09:53:18 -0300 Subject: [PATCH] Reduce docker image size (#394) Reduce docker image size based on: https://cadu.dev/reduce-your-docker-images-an-example-with-ruby/ --- .dockerignore | 9 +++++ docker-compose.production.yaml | 3 +- docker-compose.yaml | 1 + docker/Dockerfile | 72 ++++++++++++++++++++++++++-------- docker/entrypoints/rails.sh | 5 +-- docker/entrypoints/webpack.sh | 4 +- 6 files changed, 71 insertions(+), 23 deletions(-) diff --git a/.dockerignore b/.dockerignore index 1ed31c713..46b3b7b8a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,16 @@ .bundle .env .env.* +.git +.gitignore +docker-compose.* +docker/Dockerfile +docker/dockerfiles +log +storage public/system +tmp +.codeclimate.yml public/assets public/packs node_modules diff --git a/docker-compose.production.yaml b/docker-compose.production.yaml index 037384525..af5b91ca5 100644 --- a/docker-compose.production.yaml +++ b/docker-compose.production.yaml @@ -6,7 +6,8 @@ services: context: . dockerfile: ./docker/Dockerfile args: - BUNDLE_WITHOUT: '' + BUNDLE_WITHOUT: 'development:test' + EXECJS_RUNTIME: Disabled RAILS_ENV: 'production' RAILS_SERVE_STATIC_FILES: 'true' image: chatwoot:latest diff --git a/docker-compose.yaml b/docker-compose.yaml index af006154d..eedcc1109 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -7,6 +7,7 @@ services: dockerfile: ./docker/Dockerfile args: BUNDLE_WITHOUT: '' + EXECJS_RUNTIME: 'Node' RAILS_ENV: 'development' RAILS_SERVE_STATIC_FILES: 'false' tty: true diff --git a/docker/Dockerfile b/docker/Dockerfile index e7ea6a9c2..bdddef4de 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,9 +1,11 @@ -FROM ruby:2.6.5-slim +# pre-build stage +FROM ruby:2.6.5-alpine AS pre-builder # ARG default to production settings # For development docker-compose file overrides ARGS ARG BUNDLE_WITHOUT="development:test" ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT} +ENV BUNDLER_VERSION=2.1.2 ARG RAILS_SERVE_STATIC_FILES=true ENV RAILS_SERVE_STATIC_FILES ${RAILS_SERVE_STATIC_FILES} @@ -11,22 +13,20 @@ ENV RAILS_SERVE_STATIC_FILES ${RAILS_SERVE_STATIC_FILES} ARG RAILS_ENV=production ENV RAILS_ENV ${RAILS_ENV} -RUN apt-get update \ - && apt-get -qq -y install \ - build-essential \ - curl \ - git \ - imagemagick \ - libpq-dev \ +ENV BUNDLE_PATH="/gems" + +RUN apk update \ + && apk add \ + openssl \ + tar \ + build-base \ + tzdata \ + postgresql-dev \ postgresql-client \ - && curl -L https://deb.nodesource.com/setup_12.x | bash - \ - && curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ - && echo 'deb https://dl.yarnpkg.com/debian stable main' > /etc/apt/sources.list.d/yarn.list \ - && apt-get update \ - && apt-get -qq -y install nodejs yarn \ - && gem install bundler \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* + nodejs \ + yarn \ + && mkdir -p /var/app \ + && gem install bundler RUN mkdir -p /app WORKDIR /app @@ -47,4 +47,42 @@ COPY . /app # generate production assets if production environment RUN if [ "$RAILS_ENV" = "production" ]; then \ SECRET_KEY_BASE=precompile_placeholder bundle exec rake assets:precompile; \ - fi \ No newline at end of file + fi + +# final build stage +FROM ruby:2.6.5-alpine + +ARG BUNDLE_WITHOUT="development:test" +ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT} +ENV BUNDLER_VERSION=2.1.2 + +ARG EXECJS_RUNTIME="Disabled" +ENV EXECJS_RUNTIME ${EXECJS_RUNTIME} + +ARG RAILS_SERVE_STATIC_FILES=true +ENV RAILS_SERVE_STATIC_FILES ${RAILS_SERVE_STATIC_FILES} + +ARG RAILS_ENV=production +ENV RAILS_ENV ${RAILS_ENV} +ENV BUNDLE_PATH="/gems" + +RUN apk add --update --no-cache \ + openssl \ + tzdata \ + postgresql-client \ + && gem install bundler + +RUN if [ "$RAILS_ENV" = "production" ]; then \ + rm -rf spec node_modules app/assets vendor/assets tmp/cache; \ + else apk add nodejs yarn; \ + fi + +COPY --from=pre-builder /gems/ /gems/ +COPY --from=pre-builder /app /app + +# Remove unecessary files +RUN rm -rf /gems/ruby/2.6.0/cache/*.gem \ + && find /gems/ruby/2.6.0/gems/ -name "*.c" -delete \ + && find /gems/ruby/2.6.0/gems/ -name "*.o" -delete + +WORKDIR /app diff --git a/docker/entrypoints/rails.sh b/docker/entrypoints/rails.sh index f89a59ea9..f1b1efe7f 100755 --- a/docker/entrypoints/rails.sh +++ b/docker/entrypoints/rails.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh set -x @@ -17,10 +17,9 @@ done echo "Database ready to accept connections." -YARN="yarn check --integrity" BUNDLE="bundle check" -until $YARN && $BUNDLE +until $BUNDLE do sleep 2; done diff --git a/docker/entrypoints/webpack.sh b/docker/entrypoints/webpack.sh index 1a9c07fcd..d50ff1b76 100755 --- a/docker/entrypoints/webpack.sh +++ b/docker/entrypoints/webpack.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh set -e rm -rf /app/tmp/pids/server.pid @@ -17,4 +17,4 @@ done echo "Ready to run webpack development server." -exec "$@" \ No newline at end of file +exec "$@"