Reduce docker image size (#394)

Reduce docker image size based on:  https://cadu.dev/reduce-your-docker-images-an-example-with-ruby/
This commit is contained in:
Cadu Ribeiro 2019-12-31 09:53:18 -03:00 committed by Sojan Jose
parent 7c4cc68561
commit 434d6c2656
6 changed files with 71 additions and 23 deletions

View file

@ -1,7 +1,16 @@
.bundle .bundle
.env .env
.env.* .env.*
.git
.gitignore
docker-compose.*
docker/Dockerfile
docker/dockerfiles
log
storage
public/system public/system
tmp
.codeclimate.yml
public/assets public/assets
public/packs public/packs
node_modules node_modules

View file

@ -6,7 +6,8 @@ services:
context: . context: .
dockerfile: ./docker/Dockerfile dockerfile: ./docker/Dockerfile
args: args:
BUNDLE_WITHOUT: '' BUNDLE_WITHOUT: 'development:test'
EXECJS_RUNTIME: Disabled
RAILS_ENV: 'production' RAILS_ENV: 'production'
RAILS_SERVE_STATIC_FILES: 'true' RAILS_SERVE_STATIC_FILES: 'true'
image: chatwoot:latest image: chatwoot:latest

View file

@ -7,6 +7,7 @@ services:
dockerfile: ./docker/Dockerfile dockerfile: ./docker/Dockerfile
args: args:
BUNDLE_WITHOUT: '' BUNDLE_WITHOUT: ''
EXECJS_RUNTIME: 'Node'
RAILS_ENV: 'development' RAILS_ENV: 'development'
RAILS_SERVE_STATIC_FILES: 'false' RAILS_SERVE_STATIC_FILES: 'false'
tty: true tty: true

View file

@ -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 # ARG default to production settings
# For development docker-compose file overrides ARGS # For development docker-compose file overrides ARGS
ARG BUNDLE_WITHOUT="development:test" ARG BUNDLE_WITHOUT="development:test"
ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT} ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT}
ENV BUNDLER_VERSION=2.1.2
ARG RAILS_SERVE_STATIC_FILES=true ARG RAILS_SERVE_STATIC_FILES=true
ENV RAILS_SERVE_STATIC_FILES ${RAILS_SERVE_STATIC_FILES} 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 ARG RAILS_ENV=production
ENV RAILS_ENV ${RAILS_ENV} ENV RAILS_ENV ${RAILS_ENV}
RUN apt-get update \ ENV BUNDLE_PATH="/gems"
&& apt-get -qq -y install \
build-essential \ RUN apk update \
curl \ && apk add \
git \ openssl \
imagemagick \ tar \
libpq-dev \ build-base \
tzdata \
postgresql-dev \
postgresql-client \ postgresql-client \
&& curl -L https://deb.nodesource.com/setup_12.x | bash - \ nodejs \
&& curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ yarn \
&& echo 'deb https://dl.yarnpkg.com/debian stable main' > /etc/apt/sources.list.d/yarn.list \ && mkdir -p /var/app \
&& apt-get update \ && gem install bundler
&& apt-get -qq -y install nodejs yarn \
&& gem install bundler \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
RUN mkdir -p /app RUN mkdir -p /app
WORKDIR /app WORKDIR /app
@ -47,4 +47,42 @@ COPY . /app
# generate production assets if production environment # generate production assets if production environment
RUN if [ "$RAILS_ENV" = "production" ]; then \ RUN if [ "$RAILS_ENV" = "production" ]; then \
SECRET_KEY_BASE=precompile_placeholder bundle exec rake assets:precompile; \ SECRET_KEY_BASE=precompile_placeholder bundle exec rake assets:precompile; \
fi 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

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/bin/sh
set -x set -x
@ -17,10 +17,9 @@ done
echo "Database ready to accept connections." echo "Database ready to accept connections."
YARN="yarn check --integrity"
BUNDLE="bundle check" BUNDLE="bundle check"
until $YARN && $BUNDLE until $BUNDLE
do do
sleep 2; sleep 2;
done done

View file

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/sh
set -e set -e
rm -rf /app/tmp/pids/server.pid rm -rf /app/tmp/pids/server.pid
@ -17,4 +17,4 @@ done
echo "Ready to run webpack development server." echo "Ready to run webpack development server."
exec "$@" exec "$@"