A question which always gets asked regarding the union layered file system for images is whether the intermediary image layers can be merged into one? The answer is yes, you can –squash image layers during a build from a Dockerfile.

The attached video shows 2 example builds of the same Dockerfile, one is standard, the second using –squash to merge the layers.

Merge image layers in Docker

Example run script:

docker build -t NewImageNameHere . --squash

To view the image layers related to the newly created image you can run:

docker history NewImageNameHere

This will show all the layers created from each instruction in the dockerfile but the Imageid is “missing” (only 1 layer / Imageid now exists) and there will be a comment to indicate the merge has occurred and only 1 image layer now exists:

Output Without –squash

Output from Without --squash

Output With –squash (and comment showing merge)

Output from With --squashComment showing merge information