Squash the Image

Examine the advantages and disadvantages of squashing the Docker image and the use of no-install-recommends.

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.

Advantages

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.

Disadvantages

On the negative side, squashed images do not share image layers. This can result in storage inefficiencies and larger push and pull operations.

Add the --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.

Get hands-on with 1200+ tech skills courses.