Skip to main content

What is a layer in a Docker image?

A layer in a Docker image is a part of the image that is created when each instruction in a Dockerfile is executed (for example: FROM, RUN, COPY). Each layer contains the file system changes that are added on top of the previous ones.

How to understand this at the junior level

  • An image is built like a cake made of layers - one layer after another.
  • If you change one command, Docker rebuilds only the corresponding layer and takes the rest from the cache, which is why the build is fast.
  • All images reuse layers. For example, different images based on Ubuntu can share a common base layer, saving space.

Important properties of layers

PropertyExplanation
Read-onlyA layer cannot be changed - only a new one can be added on top
CacheableIf a layer already exists, Docker does not rebuild it
SharedOne layer can be used by many images

In one phrase: A layer is a separate, immutable piece of an image, created from a Dockerfile command, which Docker can cache and reuse.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.