When I fetch a Docker image from a repository I see that it pulls a bunch of layers with some ids, but when I try to run them using docker run it tells me that it can't find it.
Can I run a certain layer of a Docker image that I can see through docker history < image_id >, like a paleontologist digging to find something interesting?
What I tried:
docker pull ruby
Using default tag: latest
latest: Pulling from library/ruby
693502eb7dfb: Already exists
081cd4bfd521: Already exists
5d2dc01312f3: Already exists
54a5f7da9a4f: Pulling fs layer
168cf3f33330: Pulling fs layer
021d84fef638: Pulling fs layer
168c3c107cd1: Waiting
f001b782a027: Waiting
Then:
docker run --rm -it 5d2dc01312f3 bash
Unable to find image '5d2dc01312f3:latest' locally
The question popped to me when I was pulling an image that I have build and published. I could see it clearly that one layer had almost 1.2 GB of size. It was the layer where I copied some files from the machine I used to build the image and on the next layer I cleaned and deleted some of those files as I was cleaning the image, but the total size of the image was about 1.5 GB which means that the big fat layer is of 1.2 GB is there. I just want to see if I can peek into that fat layer.
So many years ahead, I came back to answer this question. To peek at the intermediate layers the best and easiest solution is to export the docker image into archived format and than every layer will be a tar on its own inside the bigger tar which is of the docker image.
Let's take for example docker image of tomcat:9.0 To export it into tar format:
docker save tomcat:9.0 >tomcat-9.0.tar
When exported, inside it there can be found many Layers , each one is archived in its own tar file:
tar -tvf tomcat-9.0.tar
drwxr-xr-x 0/0 0 2022-09-02 12:41 1fdef6bff31128d5a1a41bdd5fea06be032ef59fd985da09842a252310b4afa3/
-rw-r--r-- 0/0 3 2022-09-02 12:41 1fdef6bff31128d5a1a41bdd5fea06be032ef59fd985da09842a252310b4afa3/VERSION
-rw-r--r-- 0/0 482 2022-09-02 12:41 1fdef6bff31128d5a1a41bdd5fea06be032ef59fd985da09842a252310b4afa3/json
-rw-r--r-- 0/0 20891136 2022-09-02 12:41 1fdef6bff31128d5a1a41bdd5fea06be032ef59fd985da09842a252310b4afa3/layer.tar
drwxr-xr-x 0/0 0 2022-09-02 12:41 2ecda9f325b8b38fc5e0a2f1fed4526c517c087e909875abdf1ee4fbcab37b30/
-rw-r--r-- 0/0 3 2022-09-02 12:41 2ecda9f325b8b38fc5e0a2f1fed4526c517c087e909875abdf1ee4fbcab37b30/VERSION
-rw-r--r-- 0/0 482 2022-09-02 12:41 2ecda9f325b8b38fc5e0a2f1fed4526c517c087e909875abdf1ee4fbcab37b30/json
-rw-r--r-- 0/0 329143808 2022-09-02 12:41 2ecda9f325b8b38fc5e0a2f1fed4526c517c087e909875abdf1ee4fbcab37b30/layer.tar
drwxr-xr-x 0/0 0 2022-09-02 12:41 2fe9b9ee40730bab9cf86153dd37fbddb4990dd09ba5b9f22e55eeae13063fe8/
-rw-r--r-- 0/0 3 2022-09-02 12:41 2fe9b9ee40730bab9cf86153dd37fbddb4990dd09ba5b9f22e55eeae13063fe8/VERSION
-rw-r--r-- 0/0 406 2022-09-02 12:41 2fe9b9ee40730bab9cf86153dd37fbddb4990dd09ba5b9f22e55eeae13063fe8/json
-rw-r--r-- 0/0 80354304 2022-09-02 12:41 2fe9b9ee40730bab9cf86153dd37fbddb4990dd09ba5b9f22e55eeae13063fe8/layer.tar
drwxr-xr-x 0/0 0 2022-09-02 12:41 3fff0862317ff87f1d757557325c158d405afd7fd98f31544ce10cf3ad2bdaa7/
-rw-r--r-- 0/0 3 2022-09-02 12:41 3fff0862317ff87f1d757557325c158d405afd7fd98f31544ce10cf3ad2bdaa7/VERSION
-rw-r--r-- 0/0 482 2022-09-02 12:41 3fff0862317ff87f1d757557325c158d405afd7fd98f31544ce10cf3ad2bdaa7/json
-rw-r--r-- 0/0 3072 2022-09-02 12:41 3fff0862317ff87f1d757557325c158d405afd7fd98f31544ce10cf3ad2bdaa7/layer.tar
drwxr-xr-x 0/0 0 2022-09-02 12:41 5cab171b0d1115044b08690329f32a7d99a546777097e935733a837100ef34c5/
-rw-r--r-- 0/0 3 2022-09-02 12:41 5cab171b0d1115044b08690329f32a7d99a546777097e935733a837100ef34c5/VERSION
-rw-r--r-- 0/0 482 2022-09-02 12:41 5cab171b0d1115044b08690329f32a7d99a546777097e935733a837100ef34c5/json
-rw-r--r-- 0/0 3072 2022-09-02 12:41 5cab171b0d1115044b08690329f32a7d99a546777097e935733a837100ef34c5/layer.tar
drwxr-xr-x 0/0 0 2022-09-02 12:41 6b87b21c947802c9f53a614692b4369cc2d7aa951f64bf28cd1f8713e191894e/
-rw-r--r-- 0/0 3 2022-09-02 12:41 6b87b21c947802c9f53a614692b4369cc2d7aa951f64bf28cd1f8713e191894e/VERSION
-rw-r--r-- 0/0 482 2022-09-02 12:41 6b87b21c947802c9f53a614692b4369cc2d7aa951f64bf28cd1f8713e191894e/json
-rw-r--r-- 0/0 50671616 2022-09-02 12:41 6b87b21c947802c9f53a614692b4369cc2d7aa951f64bf28cd1f8713e191894e/layer.tar
drwxr-xr-x 0/0 0 2022-09-02 12:41 895adbb30677e7148af6d01e295347bd6ddc7cd5d929ee8fc3d03aedac5fa857/
-rw-r--r-- 0/0 3 2022-09-02 12:41 895adbb30677e7148af6d01e295347bd6ddc7cd5d929ee8fc3d03aedac5fa857/VERSION
-rw-r--r-- 0/0 2588 2022-09-02 12:41 895adbb30677e7148af6d01e295347bd6ddc7cd5d929ee8fc3d03aedac5fa857/json
-rw-r--r-- 0/0 2048 2022-09-02 12:41 895adbb30677e7148af6d01e295347bd6ddc7cd5d929ee8fc3d03aedac5fa857/layer.tar
-rw-r--r-- 0/0 12431 2022-09-02 12:41 d4488b7f8c9b73c0f2e91f5e37990905ae3d34d55ea893b5e70b76fc7a2db1b7.json
-rw-r--r-- 0/0 661 1970-01-01 01:00 manifest.json
-rw-r--r-- 0/0 86 1970-01-01 01:00 repositories
To see and peek at the content of each layer, all what is left now is to decompress the tomcat tar and than decompress the tar of the targeted layer.
tar -xvf tomcat-9.0.tar
tar -tvf 6b87b21c947802c9f53a614692b4369cc2d7aa951f64bf28cd1f8713e191894e/layer.tar
drwxr-xr-x 0/0 0 2022-09-02 07:24 etc/
drwxr-xr-x 0/0 0 2022-09-02 07:24 etc/ca-certificates/
-rwxr-xr-x 0/0 0 1970-01-01 01:00 etc/ca-certificates/.wh..wh..opq
drwxr-xr-x 0/0 0 2021-10-16 18:09 etc/ca-certificates/update.d/
-rw-r--r-- 0/0 5529 2022-09-02 07:24 etc/ca-certificates.conf
drwxr-xr-x 0/0 0 2022-09-02 07:24 etc/default/
-rw-r--r-- 0/0 35 2022-09-02 07:24 etc/default/locale
drwxr-xr-x 0/0 0 2022-09-02 07:24 etc/fonts/
-rwxr-xr-x 0/0 0 1970-01-01 01:00 etc/fonts/.wh..wh..opq
drwxr-xr-x 0/0 0 2022-09-02 07:24 etc/fonts/conf.avail/
[...]