Best way to reduce the size of a custom Docker image
<p>I've built a custom Docker image based off an <a href="https://hub.docker.com/_/php/" rel="noreferrer">official PHP FPM image</a> <code>php:7.0.14-fpm-alpine</code> </p>

<p>I wanted to keep the size of the image small, so I went for the official <code>alpine</code> PHP-FPM version as it weighs only <strong>27 MB</strong>.</p>

<p>I installed only few additional packages through my <code>Dockerfile</code>, and the image grew in size to as much as <strong>277.5 MB</strong>. Here is my Dockerfile:</p>

<pre><code>FROM php:7.0.14-fpm-alpine

COPY ./config/www-pool.conf /usr/local/etc/php-fpm.d/www.conf
COPY ./scripts/download-composer.sh /root/download-composer.sh

WORKDIR /root

RUN chmod +x download-composer.sh \
    &amp;&amp; ./download-composer.sh \
    &amp;&amp; mv composer.phar /usr/local/bin/composer

RUN ["mkdir", "/var/log/php-fpm"]

RUN apk --update add \
      autoconf g++ make \
      openssl-dev \
      libxml2-dev

RUN pecl install \
        xdebug \
        mongodb

RUN docker-php-ext-enable \
        xdebug.so \
        mongodb.so

RUN  docker-php-ext-install \
        pdo_mysql \
        soap

RUN addgroup sudo
RUN adduser -S luqo33 -G sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' &gt;&gt; /etc/sudoers
</code></pre>

<p>277.5 MB is a ten-fold increase in size compared to the base image. Apart from Composer, all I needed were several PHP extensions:</p>

<ul>
<li>mongodb</li>
<li>xdebug</li>
<li>pdo</li>
<li>soap</li>
</ul>

<p>I'm not sure what contributed the most to increasing the size of my image so much. I suspect that it might be due to the <code>dev</code> dependencies that needed to be installed in order to successfully run <code>pecl</code> (<code>openssl-dev</code>, <code>libxml2-dev</code>), and that might have installed their own tree of dependencies.</p>

<p>Could you please advise on how I can reduce the size of my custom PHP-FPM image and still keep the necessary extensions?</p>