Docker Layers
Updated Mar 11, 2022 ·
Overview
Docker images are made up of layers. Each layer represents a change or command in the Dockerfile. Layers can be cached or reused to optimize builds.
- Layers referencing common libraries (e.g.,
pip install requests
) - These libraries can be reused across images.
- Order and specifics of commands in the Dockerfile affect layer reuse.
Example:
RUN apt-get update
RUN pip install requests
Why Care About Layers?
Reusing layers improves efficiency.
- Faster builds: Cached layers reduce the time needed to build an image.
- Smaller storage use: Shared layers save disk space.
Inspecting Docker Image Layers
To examine layers in an image, use the docker image inspect
command.
- Provides image details: configuration, architecture, OS, and layers.
- Look for the
RootFS Layers
section to see the layers.
Example:
docker image inspect <image-id>
Example: Inspecting Postgres Layers
The docker image inspect
command shows layers in a Postgres image.
Command:
docker image inspect postgres:latest
Key details:
RootFS
section lists all layers by their IDs.- Layers represent changes made to build the image.
Using jq
for JSON Parsing
The jq
tool simplifies analyzing JSON outputs, like those from docker image inspect
.
- Parses and queries specific sections of JSON data.
- Helps extract only the needed information.
Example usage:
docker image inspect <image-id> | jq '.'
jq
Recipes with Docker
Use jq
to query specific image data.
-
To fetch the
RootFS
layers:docker image inspect <image-id> | jq '.[0].RootFS'
-
To count the number of layers:
docker image inspect <image-id> | jq '.[0].RootFS.Layers | length'
Recipes like these make it easier to extract targeted data from complex JSON outputs.
Advanced jq
Recipe Example
Count layers in an image:
- Use
jq
to locateLayers
underRootFS
. - Calculate the array length to determine the number of layers.
Command:
docker image inspect <image-id> | jq '.[0].RootFS.Layers | length'
Output includes the number of layers, demonstrating the flexibility of jq
.