-1

This is my old custom Dockerfile, and I want to make it a lot slimmer:

FROM php:7.2-apache

RUN \
    apt-get update \
    && apt-get -y --no-install-recommends install \
        # composer dependencies
        git unzip openssh-client \
        # utils for scripts
        sudo \
        # for the 'zip' PHP extension
        libghc-zlib-dev \
        # for avoiding 'zip' PHP extension's "deprecated" warnings
        libzip-dev \
        # for 'soap' PHP extension
        libxml2-dev \
        # for 'gd' PHP extension
        libpng-dev \
        # cleanup cache
        && apt-get clean \
        && rm -rf /var/lib/apt/lists/* \
    # installing composer
    && curl -sS https://getcomposer.org/installer | \
       php -- --install-dir=/usr/local/bin --filename=composer \
    # for parallel composer install
    && composer global require hirak/prestissimo --no-plugins --no-scripts \
    # PHP extensions special settings
    # (to avoid "deprecated" warnings)
    && docker-php-ext-configure zip --with-libzip \
    # install PHP extensions
    && docker-php-ext-install \
        # mysql
        pdo_mysql mysqli \
        # zip, needed for xlsx processing
        zip \
        # for soap APIs
        soap bcmath \
        # for phpoffice/phpspreadsheet
        gd \
        # for symfony/polyfill-iconv
        intl \
        # for caching
        opcache

I checked the total image size after some points:

  • 380M after php:7.2-apache
  • 408M after git unzip openssh-client
  • 408M after sudo
  • 962M after libghc-zlib-dev -> this is plus 554M
  • 962M after libzip-dev
  • 1.1G after libxml2-dev -> this is plus 200M
  • 1.1G after libpng-dev
  • 1.11G TOTAL at the end

As I see these are taking up most of the space: libghc-zlib-dev, libxml2-dev.

  • Is there any slimmer solution to this?
  • Can I delete something that is needed only at build time?
  • Can I replace these with something smaller?
  • Do I really need a 500M package (libghc-zlib-dev) to read .xlsx files with phpoffice/phpspreadsheet?

EDIT:

I did some experimenting after @Jakumi's comments, and the result is that I removed libghc-zlib-dev and added zlib1g-dev and still everything seem to be working, while the image size dropped from 1.11G to 542M

Some useful links I found while experimenting:

lewis
  • 33
  • 4
  • I'm fascinated that you install so many -dev packages, which in general are definitely larger than their non-dev counterparts, especially since - I believe - there are pre-compiled php modules that might provide those libraries without compiling them in (or keeping the -dev packages beyond that point ...) but it probably takes some testing to find that out. I'm certainly not familiar with building php containers though – Jakumi Sep 19 '19 at 21:58
  • @Jakumi thank you for your comment. I do not have experience with `apt-get` commands, but from what you said I tried to install the packages without the `-dev` in their names, but i received these errors: `E: Unable to locate package libghc-zlib`, `E: Unable to locate package libzip`, `E: Unable to locate package libpng`. I think there must be a non-dev equivalent of these packages, but I don't really know how to find them. – lewis Sep 20 '19 at 06:31

1 Answers1

0

the script you're running looks like lots of stuff is compiled on the spot.

I'm not quite certain, if that actually is intentional on your part. not trying to offend, but I'm guessing that script is either copied or you inherited it.

For simplicity's sake, my approach would be to not compile stuff in a docker container, unless really beneficial/necessary, and trusting to some degree in the system's package manager.

for example: zip functions are provided in the php-zip package, already compiled to be used as a module in php. xml functions are provided in the php-xml package, as well already compiled. similarly for many other packages. (I don't know if installing these packages will automatically enable them, I wouldn't be surprised either way. If however they are not enabled by default, you would have to write a script that does enable them... but I'm quite confident, you can find tutorials or something). Compiled libraries are way smaller than the libraries plus their source code (and the source code of compiled in dependencies).

since I don't know your exact project, maybe it does need to compile all that for whatever reason, maybe it's to get the last percent (or twenty...) of performance, so all lines in that script might have a very good reason.

So in the end, it comes down to trial and error.

to answer your questions:

  • Is there any slimmer solution to this?

most likely, using pre-compiled stuff

  • Can I delete something that is needed only at build time?

most likely, you theoretically only need the compiled libraries

  • Can I replace these with something smaller?

see above, but might have unforeseen consequences. if you care about performance, benchmark performance with the original and the pre-compiled version. for checking correctness (if you doubt that) send all requests to both the original and the pre-compiled version and check identity of output.

  • Do I really need a 500M package (libghc-zlib-dev) to read .xlsx files with phpoffice/phpspreadsheet?

probably not

Overall: using pre-compiled libraries can have a (negative) performance impact, you might have stuff compiled in, that you don't need. However, I would not expect the performance gains worth the time and just use pre-compiled libraries. (On top: you might also choose a different docker image - maybe even an alpine one -, which doesn't need any compilation at all and already has most stuff pre-installed and might still be smaller - looking at the assumed source dockerfile you extend, the libraries you add may already been compiled in and you're just recompiling them ...)

This is obviously an opinionated answer. There are certainly those who like to compile stuff themselves and there absolutely might be very good reasons to do so.

Jakumi
  • 8,043
  • 2
  • 15
  • 32
  • Thank you for your reply! I did some experimenting, and the reult is that I removed `libghc-zlib-dev` and added `zlib1g-dev` and still everything seem to be working, while the image size dropped from 1.11G to 542M :D – lewis Sep 20 '19 at 10:58