For years, I have been under the impression that every single instruction in the Docker file creates one image layer. This is reinforced by the official documentation at: https://docs.docker.com/build/cache/
However, in the official documentation itself, I see another page where it is specifically mentioned that the only DockerFile instructions to create image layers are: ADD,COPY and RUN. As per this page, rest of the DockerFile instructions just create some intermediate images and hence do not contribute to the overall size of the image.
Here is the page which mentions this: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#minimize-the-number-of-layers
I am confused now. What is the actual behavior with the latest version of Docker engine?
As a general statement, any Dockerfile instruction that modifies the file system creates a new layer.
In particular, the instructions RUN
, COPY
, ADD
mostly contribute to the size of the final Docker image and always create layers.
Some instructions, for example the ones that starts with LABEL
, ENTRYPOINT
, CMD
directives, don't modify the filesystem since they just add metadata or configurations to the image, so they don't add any layers that increased the file size of the Docker image but only create temporary intermediate layers with 0B in size.
You can use the command history:
docker history <image-name>
to check the details about layers that compose the image, the instructions and their size.