Skip to content

Commit

Permalink
Remove unnecessary base dependencies that bloat layer size
Browse files Browse the repository at this point in the history
This change reduces the layer for this `RUN` command from 1GB to 678MB
as shown in the `docker history <image name>` output.
  • Loading branch information
tgxworld committed Dec 18, 2024
1 parent 12153f7 commit 1ae1eca
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions image/base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ RUN gpg --import /tmp/nginx_public_keys.key &&\
rm /tmp/nginx_public_keys.key &&\
/tmp/install-nginx

FROM builder AS thpfoff_builder

# This tool allows us to disable huge page support for our current process
# since the flag is preserved through forks and execs it can be used on any
# process
ADD thpoff.c /src/thpoff.c
RUN gcc -o /usr/local/sbin/thpoff /src/thpoff.c && rm /src/thpoff.c

FROM discourse/ruby:3.3.6-${DEBIAN_RELEASE}-slim AS discourse_dependencies

ARG DEBIAN_RELEASE
Expand All @@ -51,14 +59,14 @@ RUN --mount=type=tmpfs,target=/var/log \
echo "debconf debconf/frontend select Teletype" | debconf-set-selections; \
apt-get -y update && DEBIAN_FRONTEND=noninteractive apt-get -y install gnupg sudo curl fping locales \
ca-certificates rsync \
cmake g++ pkg-config patch \
libxslt-dev libcurl4-openssl-dev \
libssl-dev libyaml-dev libtool \
patch \
libcurl4-openssl-dev \
libssl-dev libyaml-dev \
libpcre3 libpcre3-dev zlib1g zlib1g-dev \
libxml2-dev gawk parallel \
gawk \
libreadline-dev anacron wget \
psmisc whois brotli libunwind-dev \
libtcmalloc-minimal4 cmake \
libtcmalloc-minimal4 \
pngcrush pngquant ripgrep poppler-utils \
# imagemagick runtime dependencies
libheif1 libjbig0 libtiff6 libpng16-16 libfontconfig1 \
Expand Down Expand Up @@ -133,11 +141,7 @@ RUN gem install pups --force &&\
mkdir -p /pups/bin/ &&\
ln -s /usr/local/bin/pups /pups/bin/pups

# This tool allows us to disable huge page support for our current process
# since the flag is preserved through forks and execs it can be used on any
# process
ADD thpoff.c /src/thpoff.c
RUN gcc -o /usr/local/sbin/thpoff /src/thpoff.c && rm /src/thpoff.c
COPY --from=thpfoff_builder /usr/local/sbin/thpoff /usr/local/sbin/thpoff

# clean up for docker squash
RUN rm -fr /usr/local/share/doc &&\
Expand All @@ -162,16 +166,23 @@ RUN install -dm 0755 -o discourse -g discourse /var/www/discourse &&\
sudo -u discourse git clone --branch $DISCOURSE_BRANCH --filter=tree:0 https://github.com/discourse/discourse.git /var/www/discourse &&\
gem install bundler --conservative -v $(awk '/BUNDLED WITH/ { getline; gsub(/ /,""); print $0 }' /var/www/discourse/Gemfile.lock)

FROM discourse_slim AS discourse_release
ENV RAILS_ENV=production
FROM discourse_slim AS discourse_bundle_dependencies

RUN cd /var/www/discourse &&\
RUN apt update &&\
apt install -y cmake g++ &&\
cd /var/www/discourse &&\
sudo -u discourse bundle config --local deployment true &&\
sudo -u discourse bundle config --local path ./vendor/bundle &&\
sudo -u discourse bundle config --local without test development &&\
sudo -u discourse bundle install --jobs $(($(nproc) - 1)) &&\
find /var/www/discourse/vendor/bundle -name cache -not -path '*/gems/*' -type d -exec rm -rf {} + &&\
find /var/www/discourse/vendor/bundle -name tmp -type d -exec rm -rf {} +

FROM discourse_slim AS discourse_release
ENV RAILS_ENV=production

COPY --from=discourse_bundle_dependencies /var/www/discourse/vendor/bundle /var/www/discourse/vendor/bundle
COPY --from=discourse_bundle_dependencies /home/discourse/.bundle /home/discourse/.bundle

RUN cd /var/www/discourse &&\
sudo -u discourse /bin/bash -c 'if [ -f yarn.lock ]; then yarn install --frozen-lockfile && yarn cache clean; else pnpm install --frozen-lockfile; fi'

0 comments on commit 1ae1eca

Please sign in to comment.