Update: Since version 17.05, docker provided a simple way to build smaller image sizes https://docs.docker.com/develop/develop-images/multistage-build/
We were running our development environment with a very heavy debian image of 890MB. So, we tried to use alpine linux for a lightweight image. Our first build was a 360mb but alpine perspective promise sizes between 50~70mb.
Tip 1: this will be the last improvement but the simplest one, join every RUN inside one instruction saving up some layers
RUN apk updateRUN apk add libmemcachedRUN apk add curl
to
RUN apk update && add libmemcached curl
If you need to break line for better understanding, it can be done this way:
RUN apk update && add libmemcached \curl && wget http://speedtest.ftp.otenet.gr/files/test100k.db
Tip 2: apk –virtual and del
Separate needed libs for your image from needed libs only for compilation with the virtual flag and give a name to this group of packages:
RUN apk add autoconf gcc make g++ zlib-dev file g++ libc-dev pkgconf libmemcached-dev
to
RUN apk add --virtual .build-deps autoconf gcc make g++ zlib-dev file g++ libc-dev pkgconf libmemcached-dev \
&& apk del .build-deps && rm -rf tmp/*
The last instruction will remove the package group and temp files.
Tip 3: Any download and compilation must be executed inside /tmp
sed -i "s/\ \-n\ / /" $(which pecl) && cd /usr/local
&& pecl download memcached && tar -xf $(ls -1 memcached*); cd "$(ls -d ./memcache*|grep -v "\.tar")" && phpize && ./configure --disable-memcached-sasl --with-php-config=/usr/bin/php-config --includedir=/usr/include --with-libdir=/usr/include --enable-memcached && make && make install
to
sed -i "s/\ \-n\ / /" $(which pecl) && cd /tmp
&& pecl download memcached && tar -xf $(ls -1 memcached*); cd "$(ls -d ./memcache*|grep -v "\.tar")" && phpize && ./configure --disable-memcached-sasl --with-php-config=/usr/bin/php-config --includedir=/usr/include --with-libdir=/usr/include --enable-memcached && make && make install
At this moment the image size is about 294mb, but alpine promises extreme reduction. Investigating our compilation we find that original path was /usr/local, so we changed to /tmp
For every instruction not cleaned, each layer replicates those files like a domino effect inflating image size and duplicating it when using FROM to extend with new features. After that change, the image size is about amazing 56.99mb
Simple hints that make all the differences when working with multiple containers.
repository: https://github.com/luzeduardo/alpine-php-fpm
For any doubt, correction and improvement let me know