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
.env
.env.*
.git
.gitignore
docker-compose.*
docker/Dockerfile
docker/dockerfiles
log
storage
public/system
tmp
.codeclimate.yml
public/assets
public/packs
node_modules

View file

@ -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

View file

@ -7,6 +7,7 @@ services:
dockerfile: ./docker/Dockerfile
args:
BUNDLE_WITHOUT: ''
EXECJS_RUNTIME: 'Node'
RAILS_ENV: 'development'
RAILS_SERVE_STATIC_FILES: 'false'
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
# 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
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
@ -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

View file

@ -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 "$@"
exec "$@"