botbotbot 's blog

Reduce docker image size

Reduce Docker image size can help your to portable your contain across network, save bandwidth and storage, faster to scale.

1. Using alpine base image when possible

It is minimal Docker image based on Alpine Linux that only 5 MB. You can install packages with apk add --update <pakckages> and you can check packages that avaible on alpine packages.

Debian/Ubuntu vs Alpine

You can see how small alpine based image are. However alpine based is new born linux that not contain much packages as older linux like debian/ubuntu. There are some compiled packages and some that your have to compile with your own.

2. Using apt-get with --no-install-recommends option on Debian/Ubuntu Image

with this option, Marc Campbell can drop ~120MB for python-pip and curl packages and I can save ~700MB by using this technique and combine commands on docker-predictionio that based on sphereio/docker-predictionio.

RUN apt-get install -y --no-install-recommends <packages>

3. Combine commands to reduce layers

Example your Dockfile look like

RUN apt-get update
RUN apt-get install -y python-pip
RUN pip install predictionio

should combile commands like this

RUN apt-get update \
    && apt-get install -y python-pip \
    && pip install predictionio

that reduce image layer and image size. However your should do this when your sure with Dockerfile becuase docker build will cache a huge layer, you need to rebuild all commands if your edit any single one.

4. Remove unecessary packages/files

Remove some no longer need packages ex. python-pip curl Any SDK and compressed file that your extracted when you finish your installation.

Must remove in same layer, remove in another layer not help to reduce image size.

RUN apt-get install -y python-pip pyhton-dev \
    && pip install predictionio \
    && apt-get remove -y python-pip \
    && ap-get clean \
    && rm -rf /var/lib/apt/lists/*

5. Using docker-squash to make them smaller

I not sure about this solution that I can’t complie it on OSX. You can read more about docker-squash in References.

References::