How to reduce Docker image size

I have container running GlassFish. When I tried commit a image , it became 14GB. Later, I deleted /tmp/ in the container which was 10GB and tried to commit a image, but still the image is 14GB. It seems that deleting files from the container is not reflected in the image size. How do I fix this issue?


The problem that you might be facing is that removing files in a commit does not change the fact that the file was there in a previous image, so that previous image is still 14G.

Have a look at the "Layers" part in this article: http://woudenberg.io/reducing-docker-image-size/

To solve that issue you need to remove the things under /tmp/ within the original commit that generated them, so the image created as a result of that commit does not include them. That is easy when use dockerfiles but not sure if possible when creating images with commits.

Another thing you may be able to do is squash layers by exporting/importing the image.


Extending on @Juan Antonio's answer, having a Dockerfile doing this will not save space:

RUN apt-get install -y foo bar
...
RUN apt-get purge --auto-remove -y foo bar

What you need to do instead is:

RUN apt-get install -y foo bar && \
    && ... \
    && apt-get purge --auto-remove -y foo bar

On the down side, this means less caching as if anything changes in that large command line, Docker will have to re-run the entire set of commands.