Middle
What is a layer in Docker and what is it used for?
sobes.tech AI
Answer from AI
In Docker, a layer is a separate level of the filesystem that is created each time an instruction is executed in the Dockerfile (for example, RUN, COPY, ADD). Each layer contains changes compared to the previous layer, such as added or modified files.
Layers are needed for:
- Caching: During image build, Docker reuses unchanged layers, speeding up the build.
- Storage optimization: Layers can be used by multiple images, saving space.
- Change management: Allows tracking and rolling back changes.
Example: if a Dockerfile first copies code and then installs dependencies, then when only the code changes, Docker will rebuild only the top layer, leaving the dependencies layer untouched.