Squash the Image
Squashing an image isn’t really a best practice, as it has pros and cons.
At a high level, Docker follows the normal process to build an image. But then, it adds an additional step that squashes everything into a single layer.
Squashing can be good in situations where images are starting to have a lot of layers, but this isn’t ideal. An example of this might be when creating a new base image that you want to build other images from in the future. This base is much better as a single-layer image.
On the negative side, squashed images do not share image layers. This can result in storage inefficiencies and larger push and pull operations.
--squash flag to the
docker image build command if you want to create a squashed image.
The figure below shows some of the inefficiencies that come with squashed images. Both images are exactly the same, except for the fact that one is squashed and the other is not. The non-squashed image shares layers with other images on the host (saving disk space), but the squashed image does not. The squashed image will also need to send every byte to Docker Hub on a
docker image push command, whereas the non-squashed image only needs to send unique layers.