1

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?

Any advice would be very much appreciated.

Thanks in advance.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Kiran Hegde
  • 680
  • 4
  • 14
  • Does this answer your question? [How to link docker images to their composing layers on the disk?](https://stackoverflow.com/questions/43070640/how-to-link-docker-images-to-their-composing-layers-on-the-disk) – Ken Y-N Aug 02 '23 at 05:35
  • 1
    Layers and Images are now separate things, so you need to adjust your mental model a bit. – Ken Y-N Aug 02 '23 at 05:36
  • I'm hazy on the details, but I have observed pretty much any command to call for a new layer, including WORKDIR, USER and ENTRY POINT. I think there is still a limit on the amount of layers, It makes sense to group things that belong together in one layer that belong together and I like pulling fewer layers, but being too frugle als doesn't make sense. It doesn't save disk space. If you want to know the details you can inspect the layers, there are tools that peel them apart. Otherwise I consider this document to be sufficient explanatory – sleepyhead Aug 02 '23 at 05:56
  • https://docs.docker.com/build/guide/layers/ – sleepyhead Aug 02 '23 at 05:56

1 Answers1

2

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.

Alez
  • 1,913
  • 3
  • 18
  • 22