Search code examples
ruby-on-railsrubydockerrails-activestoragelibvips

Huge docker image size with Ror based on ruby slim-bullseye and libvips-dev


I'm using ruby:3.1.3-slim-bullseye to dockerize a rails 7 application.

The image size is about 2Gb with no clear reason.

I'm using active storage which uses vips (that's why i use libvips-dev).

I tried to inspect image layers with docker history, the output indicates two huge layers related to these docker commands.

RUN bash -c "set -o pipefail && apt-get install -y --no-install-recommends build-essential curl git libpq-dev  "

RUN bash -c "set -o pipefail && apt-get install -y libvips-dev"

Output for docker history my_image --no-trunc,

..... other entries 


sha256:bf2ec68f81ba5fea38089b6a09d60786f296776b76f8083c9e3c94eae64533f5   24 minutes ago   |3 GID=1000 NODE_ENV=production UID=1000 /bin/sh -c bash -c "set -o pipefail && apt-get install -y libvips-dev"       667MB

sha256:1f778f131d692203d3918ea0fbc8e91c2466fadc7bd42ac8d333b8cf34cdae2b   25 minutes ago   |3 GID=1000 NODE_ENV=production UID=1000 /bin/sh -c bash -c "set -o pipefail && apt-get install -y --no-install-recommends build-essential curl git libpq-dev  "           258MB


...... other entries 

My Dockerfile

FROM ruby:3.1.3-slim-bullseye
WORKDIR /app

.... Other docker entries

#
RUN bash -c "set -o pipefail  && apt-get update "

RUN bash -c "set -o pipefail && apt-get install -y --no-install-recommends build-essential curl git libpq-dev  "

RUN bash -c "set -o pipefail && apt-get install -y libvips-dev"

RUN bash -c "set -o pipefail &&  groupadd -g \"${GID}\" ruby && useradd --create-home --no-log-init -u \"${UID}\" -g \"${GID}\" ruby"

RUN bash -c "set -o pipefail  \
  && chown ruby:ruby -R /node_modules /app \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man "

.... Other docker entries


What's wrong with apt-get install so it makes libvips-dev takes 667MB and build-essential curl git libpq-dev took 258MB ?


Solution

  • I opened an issue on ruby-vips (https://github.com/libvips/ruby-vips/issues/370#issuecomment-1716933820).

    Here are the recommandations :

    1 - Instead of installing libvips-dev, we can use plain libvips. (Don't know the exact reason for using the libvips-dev initially)

    2 - Add --no-install-recommends to the command

    So the command is apt-get install -y --no-install-recommends libvips

    The image layer size now is 130MG which is ok comparing to 667MB