- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Find the layers and layer sizes for each Docker image?
Introduction
By using containerization, we can quickly set up and configure our deployment environments, which helps us save time and resources. With the goal of "write once, deploy anywhere" in mind, containerization can help us streamline the process of deploying modern applications, which can be complex.
Finding the layers and layer sizes for Docker images
In this article, we will explore how to find the layers and layer sizes for each Docker image. This can be useful for identifying large layers that may be contributing to the overall size of the image, and for determining which layers can be removed or optimized to reduce the image size.
Methods
We may employ the following techniques to determine the layers and layer sizes for each Docker image −
Using Dockerfile
Using Docker Image Inspect Command
Let us understand each of these in detail and see them in action with examples.
Using Dockerfile
Using the Dockerfile method, you can do the following actions to determine the layers and layer sizes for each Docker image −
Example
Step 1 − Create a new directory for your project and navigate to it −
$ mkdir directoryname $ cd directoryname
Step 2 − Make a file called ‘Dockerfile’ with the below content in this new directory −
FROM alpine:latest RUN apk add --no-cache curl
This contains the instructions for building your image as well as the basic image.
Step 3 − Run the docker build command in the terminal to build the image using the Dockerfile −
docker build -t myimage .
Step 4 − To inspect the layers and layer sizes, run the docker images command in the terminal to list all the available images on the system −
docker images
Output
REPOSITORY TAG IMAGE ID CREATED SIZE myimage latest 499a9ddb3acd 2 minutes ago 9.29MB docker-apps latest eb6dda32a60d 3 hours ago 176MB docker101tutorial latest 1ebfcd22ec64 3 hours ago 47MB ubuntu latest 6b7dfa7e8fdb 4 weeks ago 77.8MB alpine/git latest 22d84a66cda4 7 weeks ago 43.6MB
Step 5 − Run the docker inspect command in the terminal to inspect the image and get its details, including the layers and their sizes.
docker inspect IMAGE_ID
Output
docker inspect 499a9ddb3acd [ { "Id": "sha256:499a9ddb3acdf2117fdb4df826e0782d05d01aa8d1c0574c96e7bed6fbbcf698", "RepoTags": [ "myimage:latest" ], "RepoDigests": [], "Parent": "", "Comment": "buildkit.dockerfile.v0", "Created": "2023-01-08T07:49:58.294267525Z", "Container": "", "ContainerConfig": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": null, "Cmd": null, "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "DockerVersion": "", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh" ], "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 9292948, "VirtualSize": 9292948, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/e4c1f1bb76c66c5d1781a775c8a2ca9338065ea0b398aeaed4230a00a15d2952/diff", "MergedDir": "/var/lib/docker/overlay2/osfaf1c97lm7awgfbaqibrt6p/merged", "UpperDir": "/var/lib/docker/overlay2/osfaf1c97lm7awgfbaqibrt6p/diff", "WorkDir": "/var/lib/docker/overlay2/osfaf1c97lm7awgfbaqibrt6p/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:ded7a220bb058e28ee3254fbba04ca90b679070424424761a53a043b93b612bf", "sha256:19bc0373c06b6727c115ef34961486b1a9f3b7c8ccbd3441c4b108aeef2b338d" ] }, "Metadata": { "LastTagTime": "2023-01-08T07:49:58.412703277Z" } } ]
Using Docker Image Inspect Command
The "docker image inspect" command allows us to explore comprehensive details about a Docker image, such as its layers and sizes.
The steps below can be used to obtain the layers and layer sizes for a Docker image −
Step 1 − Go to the directory containing your Docker images by opening a terminal or command prompt.
Step 2 − To view all of the Docker images that are accessible, run the command below in your terminal −
docker image ls
Output
REPOSITORY TAG IMAGE ID CREATED SIZE myimage latest 499a9ddb3acd 17 minutes ago 9.29MB docker-apps latest eb6dda32a60d 3 hours ago 176MB docker101tutorial latest 1ebfcd22ec64 3 hours ago 47MB ubuntu latest 6b7dfa7e8fdb 4 weeks ago 77.8MB alpine/git latest 22d84a66cda4 7 weeks ago 43.6MB
Step 3 − Choose the image you want to examine and make a note of its ID from your previous output. We'll utilize the image with ID “499a9ddb3acd” in this example.
Step 4 − Run the following command in the terminal to inspect the image −
docker image inspect 499a9ddb3acd
Output
docker image inspect 499a9ddb3acd [ { "Id": "sha256:499a9ddb3acdf2117fdb4df826e0782d05d01aa8d1c0574c96e7bed6fbbcf698", "RepoTags": [ "myimage:latest" ], "RepoDigests": [], "Parent": "", "Comment": "buildkit.dockerfile.v0", "Created": "2023-01-08T07:49:58.294267525Z", "Container": "", "ContainerConfig": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": null, "Cmd": null, "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "DockerVersion": "", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh" ], "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 9292948, "VirtualSize": 9292948, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/e4c1f1bb76c66c5d1781a775c8a2ca9338065ea0b398aeaed4230a00a15d2952/diff", "MergedDir": "/var/lib/docker/overlay2/osfaf1c97lm7awgfbaqibrt6p/merged", "UpperDir": "/var/lib/docker/overlay2/osfaf1c97lm7awgfbaqibrt6p/diff", "WorkDir": "/var/lib/docker/overlay2/osfaf1c97lm7awgfbaqibrt6p/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:ded7a220bb058e28ee3254fbba04ca90b679070424424761a53a043b93b612bf", "sha256:19bc0373c06b6727c115ef34961486b1a9f3b7c8ccbd3441c4b108aeef2b338d" ] }, "Metadata": { "LastTagTime": "2023-01-08T07:49:58.412703277Z" } } ]
Step 5 − The output will be a JSON object containing detailed information about the image, including its layers and layer sizes in the terminal.
To view just the layers and layer sizes, run the following command in your terminal −
docker image inspect --format='{{json .RootFS.Layers}}' 499a9ddb3acd
A list of the layers and their sizes for the image will be produced in the output in the terminal.
Output
["sha256:ded7a220bb058e28ee3254fbba04ca90b679070424424761a53a043b93b612bf","sha256:19bc0373c06b6727c115ef34961486b1a9f3b7c8ccbd3441c4b108aeef2b338d"]
Conclusion
In this article, we explored various methods for finding the layers and layer sizes for each Docker image. By using the Dockerfile method, we can view the layers and sizes by inspecting the instructions used to build the image. The Docker history command allows us to view the history of an image and see the layers and their sizes. The Docker image inspect command also provides detailed information on the layers and sizes of an image.