In the evolving world of containerization, Docker has emerged as a tool for packaging and deploying applications. While creating and sharing Docker images has streamlined the development and deployment process.
Contents of a Docker image are important for troubleshooting, optimizing, and ensuring security in your containerized applications. This article guides on how to inspect the contents of a docker image.
Inspecting the docker image directly is not possible but we can inspect docker containers that are built on the image itself.
Methods to see docker image contents
There are two methods by which we can see the contents of the docker image. You can view the contents of the image using the container.
1. Docker Desktop
Docker Desktop is a GUI to manage images, containers, and volumes.
- Build your docker image.
- Run your container.
- Open the containers section from the docker desktop.
- Open your container (practical_banach here).
- Open the Files tabs from the top menu.
From this, we can view all the files and sub-directories in a docker container.
2. Using CLI
The command line interface allows you to view files in the shell using commands.
- Build a docker image.
- Run docker container.
Run docker container using following syntax.
docker run <image-name>
Use ‘-p’ to expose your port number.
Use ‘–detach’ to run your container in background. This will allow you to perform another operations from same terminal.
Inspect Docker Image
Using the following command you can view detailed information about an image, including its layers and filesystem changes.
docker inspect <image-name>
Save & Extract Docker Image
You can save a docker image, use the docker save command followed by the name of the image and tar file name separated by ‘>’.
docker save image-name > file-name.tar
You can extract a docker image, use the docker import command followed by the name of the tar archive and image name.
docker import file-name.tar image-name
After successful execution of extraction the sha will be visible.
i. Docker Execute
docker exec allows you to execute commands within an already deployed container, or outside of the container.
docker exec <container_name> ls /
docker exec <container_name> ls <path>
This command will list all the files in the present directory.You can change the file path to list files in the given directory given in the command.
ii. Create Text file
docker export allows you to export and create files on your local system.
docker export my-container | tar t > my-container-files.txt
You can replace my container name with your container name.
Replace my-container-files name to change the output text file name.
Conclusion
In this article, we explored CLI commands and docker desktop to inspect the image using containers. We also exported the contents of the image in a text file using the docker export command. Choosing right method is very important you can use docker desktop which is GUI to view and maintain images and container very easily. But GUI can be very helpful for development purpose. If someone wants to share and collaborate image contents one can extract data into text file using CLI and share it.
Docker Image Contents – FAQ’s
Can I modify a Docker image after inspecting its contents?
You cannot directly modify an existing image, you can create a new image based on the changes you desire.
Why do I need to inspect the contents of a Docker image?
Inspecting Docker image contents is essential for troubleshooting issues, optimizing performance, and security of your containerized applications.
Are there tools specifically designed for exploring Docker images?
Yes, tools like Dive, Skopeo, and Trivy offer a more user-friendly interface.
Are there graphical tools for exploring Docker image contents?
Yes, tools like Portainer provide a web-based graphical interface.