How can I minimize the discourse Docker image size?

The slim image still include postgres and redis.

The difference between slim and the other starts from command 37. The following commands are not executed in the slim:

ENV RAILS_ENV=production
RUN |3 DEBIAN_RELEASE=bookworm PG_MAJOR=15 DISCOURSE_BRANCH=main /bin/sh -c 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 {} + # buildkit
RUN |3 DEBIAN_RELEASE=bookworm PG_MAJOR=15 DISCOURSE_BRANCH=main /bin/sh -c 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' # buildkit

Basically, it is missing some build steps. No bundle install no pnpm install. Stuff you will eventually need.

The big pains are in command 11: 390MiB

RUN |2 DEBIAN_RELEASE=bookworm PG_MAJOR=15 /bin/sh -c 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     libpcre3 libpcre3-dev zlib1g zlib1g-dev     libxml2-dev gawk parallel     libreadline-dev anacron wget     psmisc whois brotli libunwind-dev     libtcmalloc-minimal4 cmake     pngcrush pngquant ripgrep poppler-utils     ghostscript libjbig0 libtiff6 libpng16-16 libfontconfig1     libwebpdemux2 libwebpmux3 libxext6 librsvg2-2 libgomp1     fonts-urw-base35 libheif1/${DEBIAN_RELEASE}-backports     nginx-common &&     DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends git rsyslog logrotate cron ssh-client less;     install -d /usr/share/postgresql-common/pgdg &&    curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc &&    echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt ${DEBIAN_RELEASE}-pgdg main" > /etc/apt/sources.list.d/pgdg.list;     curl -sS 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;     curl --silent --location https://deb.nodesource.com/setup_22.x | sudo bash -;     sed -i -e 's/start -q anacron/anacron -s/' /etc/cron.d/anacron;     sed -i.bak 's/$ModLoad imklog/#$ModLoad imklog/' /etc/rsyslog.conf;     sed -i.bak 's/module(load="imklog")/#module(load="imklog")/' /etc/rsyslog.conf;     dpkg-divert --local --rename --add /sbin/initctl;     sh -c "test -f /sbin/initctl || ln -s /bin/true /sbin/initctl";     apt-get -y update && DEBIAN_FRONTEND=noninteractive apt-get -y install runit socat     libpq-dev postgresql-client-${PG_MAJOR}     postgresql-${PG_MAJOR} postgresql-contrib-${PG_MAJOR} postgresql-${PG_MAJOR}-pgvector     nodejs yarn &&    mkdir -p /etc/runit/1.d # buildkit

It installs a lot of required tooling for other parts, also also postgresql:

gnupg sudo curl fping locales ca-certificates rsync cmake g++ pkg-config patch libxslt-dev libcurl4-openssl-dev libssl-dev libyaml-dev libtool libpcre3 libpcre3-dev zlib1g zlib1g-dev libxml2-dev gawk parallel libreadline-dev anacron wget psmisc whois brotli libunwind-dev libtcmalloc-minimal4 cmake pngcrush pngquant ripgrep poppler-utils ghostscript libjbig0 libtiff6 libpng16-16 libfontconfig1 libwebpdemux2 libwebpmux3 libxext6 librsvg2-2 libgomp1 fonts-urw-base35 libheif1 nginx-common git rsyslog logrotate cron ssh-client less runit socat libpq-dev postgresql-client postgresql postgresql-contrib postgresql-pgvector nodejs yarn