Skip to content

Commit

Permalink
Reduce docker image size (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
duduribeiro authored and sojan-official committed Dec 31, 2019
1 parent 7c4cc68 commit 434d6c2
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 23 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
dockerfile: ./docker/Dockerfile
args:
BUNDLE_WITHOUT: ''
EXECJS_RUNTIME: 'Node'
RAILS_ENV: 'development'
RAILS_SERVE_STATIC_FILES: 'false'
tty: true
Expand Down
72 changes: 55 additions & 17 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
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}

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
Expand All @@ -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
5 changes: 2 additions & 3 deletions docker/entrypoints/rails.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh

set -x

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docker/entrypoints/webpack.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
set -e

rm -rf /app/tmp/pids/server.pid
Expand All @@ -17,4 +17,4 @@ done

echo "Ready to run webpack development server."

exec "$@"
exec "$@"

0 comments on commit 434d6c2

Please sign in to comment.