I have been using Docker for some time while I don't have an IT background.
Now, I am making an effort to understand how the size of my docker images can be minimized by optimizing my Dockerfiles. In this sense, I run into a minimal reproducible case which I don't understand. I would be very glad if anyone could share his ideas or provide an explanation.
I start from an official centos:7 image (7e6257c9f8d8; 203MB). Then, I prepare the following Dockerfile:
FROM centos:7
RUN yum -y install nano && yum -y clean all && rm -fr /var/cache
RUN yum -y install which && yum -y clean all && rm -fr /var/cache
RUN yum -y install which && yum -y clean all && rm -fr /var/cache
The idea is to install whatever lightweight package and evaluate the impact on image size. For this I install nano first, followed by which in a different label. I add an extra attempt of installing which (this identifies there is nothing to do). Moreover, I add yum clean all statements for cleaning yum cache and, just in case (even though I just checked that the experiment result does not vary if I remove this command), I delete the /var/cache dir (this is empty in the base image).
The result is the following:
IMAGE CREATED CREATED BY SIZE
6a14537d3460 7 seconds ago /bin/sh -c yum -y install which && yum -y cl… 23.9MB
7d924cbdf819 22 seconds ago /bin/sh -c yum -y install which && yum -y cl… 24.2MB
2b5b04d37a64 42 seconds ago /bin/sh -c yum -y install nano && yum -y cle… 24.6MB
The installed size of which is 75k and the installed size of nano is 1.6M. I don't identify any additional installed dependencies.
The question is: Why each of these install commands increases the final image by a ~24MB layer even when no packages are actually installed?
Thanks in advance to the community :)