Extracting Docker image contents on local machine or spin-up another container
Vaibhav Shimpi
Application Architect | Solution Architecture | Azure | Cloud Native | .NET Core | DevOps | Angular
Sometimes there is often a need to have look at the docker image contents for debugging the application locally, checking the assembly versioning and executable code functioning into different platforms based on processors such as amd64, arm64, etc.
There are multiple ways to extract the docker image contents such as exporting the docker image contents into tar locally, extracting the running container contents using kubectl execution container login command and then using cp command.
The simplest way is to build or download the specific docker image locally using these steps -
docker login ghcr.io
It will ask for username and password. Upon authenticating successfully, the Login succeeded message will be displayed.
2. Pull the specific docker image using docker pull command mention below -
docker pull image-full-name:tag
This will pull the image from ghcr registry to your local docker environment.
3. View the list of docker images using command -
docker images
4. The docker image content can be extracted without/before running it in as container. The container should not be started or executed just to see the image contents. For instance, you might want to look for malicious content, not run it. Use "create" instead of "run" as shown below -
docker create --name="image_content_$$" image-full-name:tag
The above command will copy the provided docker image content into "image_content_$$".
5. Now, it is up-to us whether we would like to print and list the file system contents or would like to save it as a tar file on your local machine.
docker export image_content_$$ | tar temp
docker export image_content_$$ > tar file-name.tar
6. The tar file can then be extracted using in-built utility provided by MacOS or installing the winzip, 7-Zip utilities for windowOS.
P.S. -> The above commands are working on MacOS, may or may not require any tweaks on windowsOS.
Happy Coding!