Docker image size reduction
Created at 2016-06-10T19:04:41.000Z

I've never cared about the size of Docker image. But, when I was trying to dockerize gcc cross compiler, I couldn't ignore this problem.

I employed a technique explained in this blog. Here are two Dockerfile doing same things:

Dockerfile:

FROM ubuntu:14.04

RUN apt-get update && apt-get install -y gcc wget build-essential

# specify binutils/gcc version
ENV DOWNLOAD_BINUTILS=binutils-2.26
ENV DOWNLOAD_GCC=gcc-4.9.3

# download binutils/gcc and its dependencies
RUN wget -q http://ftp.gnu.org/gnu/binutils/$DOWNLOAD_BINUTILS.tar.gz
RUN tar -xzf $DOWNLOAD_BINUTILS.tar.gz

RUN wget -q ftp://ftp.gnu.org/gnu/gcc/$DOWNLOAD_GCC/$DOWNLOAD_GCC.tar.gz
RUN tar -xzf $DOWNLOAD_GCC.tar.gz

RUN cd /$DOWNLOAD_GCC && contrib/download_prerequisites

# specify TARGET
ENV TARGET=i686-elf
ENV PREFIX=/usr/local

# build binutils
RUN mkdir -p /srv/build_binutils
WORKDIR /srv/build_binutils
RUN /$DOWNLOAD_BINUTILS/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror
RUN make
RUN make install

# build gcc
RUN mkdir -p /srv/build_gcc
WORKDIR /srv/build_gcc
RUN /$DOWNLOAD_GCC/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers
RUN make all-gcc
RUN make all-target-libgcc
RUN make install-gcc
RUN make install-target-libgcc

# remove big files
RUN rm -r /$DOWNLOAD_BINUTILS /$DOWNLOAD_GCC /srv/build_binutils /srv/build_gcc

WORKDIR /

Dockerfile.slim:

FROM ubuntu:14.04

# install dependencies
RUN apt-get update                                                && \
    apt-get install -y gcc wget build-essential                   && \
    apt-get clean autoclean                                       && \
    apt-get autoremove -y                                         && \
    rm -rf /var/lib/apt /var/lib/dpkg /var/lib/cache /var/lib/log

# specify binutils/gcc version
ENV DOWNLOAD_BINUTILS=binutils-2.26
ENV DOWNLOAD_GCC=gcc-4.9.3

# specify TARGET
ENV TARGET=i686-elf
ENV PREFIX=/usr/local

# binutils
RUN wget -q http://ftp.gnu.org/gnu/binutils/$DOWNLOAD_BINUTILS.tar.gz && \
    tar -xzf $DOWNLOAD_BINUTILS.tar.gz                                && \
    mkdir -p /srv/build_binutils                                      && \
    cd /srv/build_binutils                                            && \
    /$DOWNLOAD_BINUTILS/configure --target=$TARGET --prefix="$PREFIX"    \
                        --with-sysroot --disable-nls --disable-werror && \
    make                                                              && \
    make install                                                      && \
    rm -r /$DOWNLOAD_BINUTILS /srv/build_binutils

# gcc
RUN wget -q ftp://ftp.gnu.org/gnu/gcc/$DOWNLOAD_GCC/$DOWNLOAD_GCC.tar.gz && \
    tar -xzf $DOWNLOAD_GCC.tar.gz                                        && \
    cd /$DOWNLOAD_GCC && contrib/download_prerequisites                  && \
    mkdir -p /srv/build_gcc                                              && \
    cd /srv/build_gcc                                                    && \
    /$DOWNLOAD_GCC/configure --target=$TARGET --prefix="$PREFIX"            \
                 -disable-nls --enable-languages=c,c++ --without-headers && \
    make all-gcc                                                         && \
    make all-target-libgcc                                               && \
    make install-gcc                                                     && \
    make install-target-libgcc                                           && \
    rm -r /$DOWNLOAD_GCC /srv/build_gcc

WORKDIR /
$ docker build -f Dockerfile -t hiogawa/i686-elf .
$ docker build -f Dockerfile.slim -t hiogawa/i686-elf-slim .
$ docker images
REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE
hiogawa/i686-elf        latest              85de86d956f5        6 hours ago          2.902 GB
hiogawa/i686-elf-slim   latest              588d6a8e1a3f        About a minute ago   864.8 MB

(TODO: add docker history command results)

References