fix: Reduce size of Docker image (#2779)

* fix: Ordering in Dockerfile

Folders node_modules & tmp/cache are currently not removed and thus the Docker image
size is pretty big.

I don't think we can remove app/assets & vendor/assets though as the
app won't work without those.

* Expose port 3000 in Docker image
* Further Dockerfile optimization

Co-authored-by: Vishnu Narayanan <vishnu@chatwoot.com>
This commit is contained in:
Pascal Jufer 2021-08-12 11:40:02 +02:00 committed by GitHub
parent 21d2b4d918
commit 3d807684bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,7 +29,6 @@ RUN apk update \
&& mkdir -p /var/app \ && mkdir -p /var/app \
&& gem install bundler && gem install bundler
RUN mkdir -p /app
WORKDIR /app WORKDIR /app
COPY Gemfile Gemfile.lock ./ COPY Gemfile Gemfile.lock ./
@ -45,11 +44,20 @@ RUN yarn install
COPY . /app COPY . /app
# creating a log directory so that image wont fail when RAILS_LOG_TO_STDOUT is false
# https://github.com/chatwoot/chatwoot/issues/701
RUN mkdir -p /app/log
# 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 RAILS_LOG_TO_STDOUT=enabled bundle exec rake assets:precompile; \ SECRET_KEY_BASE=precompile_placeholder RAILS_LOG_TO_STDOUT=enabled bundle exec rake assets:precompile \
&& rm -rf spec node_modules tmp/cache; \
fi fi
# Remove unnecessary files
RUN rm -rf /gems/ruby/3.0.0/cache/*.gem \
&& find /gems/ruby/3.0.0/gems/ \( -name "*.c" -o -name "*.o" \) -delete
# final build stage # final build stage
FROM ruby:3.0.2-alpine FROM ruby:3.0.2-alpine
@ -74,20 +82,13 @@ RUN apk add --update --no-cache \
imagemagick \ imagemagick \
&& gem install bundler && gem install bundler
RUN if [ "$RAILS_ENV" = "production" ]; then \ RUN if [ "$RAILS_ENV" != "production" ]; then \
rm -rf spec node_modules app/assets vendor/assets tmp/cache; \ apk add nodejs yarn; \
else apk add nodejs yarn; \
fi fi
COPY --from=pre-builder /gems/ /gems/ COPY --from=pre-builder /gems/ /gems/
COPY --from=pre-builder /app /app COPY --from=pre-builder /app /app
# Remove unnecessary files
RUN rm -rf /gems/ruby/3.0.0/cache/*.gem \
&& find /gems/ruby/3.0.0/gems/ \( -name "*.c" -o -name "*.o" \) -delete
# creating a log directory so that image wont fail when RAILS_LOG_TO_STDOUT is false
# https://github.com/chatwoot/chatwoot/issues/701
RUN mkdir -p /app/log
WORKDIR /app WORKDIR /app
EXPOSE 3000