iron-io / dockers

Uber tiny Docker images for all the things.
http://www.iron.io
MIT License
1.59k stars 139 forks source link

Inline the RUN operations so that disk space is freed #22

Closed shift closed 8 years ago

shift commented 8 years ago

The following changes for the ruy container provide a 8.00% improvement in size:

FROM iron/base

RUN apk update && apk upgrade

RUN apk add libxml2 libxslt libevent libffi glib ncurses readline openssl yaml zlib curl
RUN apk add mariadb-libs libpq
RUN apk add ruby ruby-io-console

# Clean APK cache
RUN rm -rf /var/cache/apk/*

Becomes:

FROM alpine

RUN apk update && apk upgrade \
    && apk add ca-certificates libxml2 libxslt libevent libffi glib ncurses readline \
    openssl yaml zlib curl  mariadb-libs libpq ruby ruby-io-console \
    && rm -rf /var/cache/apk/*

The output of the original image virtual size is 40.07 MB while the inlined version is 37.02 MB, in-keeping with the README's provide the smallest possible images, this is an improvement :)

This is because the layers are overlaid on each previous layer, you're currently leaving the packages in the apt RUN line layers and then saying they don't exist in the last layer, so the data is still pulled down.

In-lining the commands leaves you with only one additional layer on the base image with all of the cleanup occurring in it :).

jocubeit commented 8 years ago

Impressive @shift, not something I had thought about.

treeder commented 8 years ago

Nice, tested this out on the node image and it dropped it a good 5-6 MB. https://github.com/iron-io/dockers/blob/master/node/Dockerfile

shift commented 8 years ago

Happy to help :)

neg3ntropy commented 8 years ago

Was about to post this as well while looking at the java image. BTW this is a general rule: you just never do cleanup of temporary files in a RUN by itself, always chain it to the RUN that brought in the files in the first place. Cheers