Junior — Middle
Can you explain what a layer in Docker is and why it is important for containerization?
sobes.tech AI
Answer from AI
In Docker, a layer is a separate level of the filesystem that is created when each instruction in the Dockerfile is executed (for example, RUN, COPY, ADD). Each layer contains changes relative to the previous layer.
The significance of layers:
- Storage efficiency: Layers are cached and can be reused across different images, saving space and speeding up the build.
- Fast build: When the Dockerfile is changed, only the modified layers are rebuilt, while others are retrieved from the cache.
- Change isolation: Each layer is a separate set of changes, which simplifies management and rollback.
For example, if you have a Dockerfile:
FROM ubuntu:20.04
RUN apt-get update
RUN apt-get install -y curl
COPY . /app
Each instruction creates a separate layer. If you only change the contents of the /app folder, Docker will rebuild only the last layer, while the previous ones are taken from the cache.