wagoodman / dive

A tool for exploring each layer in a docker image
MIT License
45.7k stars 1.74k forks source link

[FEATURE REQUEST] - Add view to see contents of file #336

Open ondrovic opened 3 years ago

ondrovic commented 3 years ago

Any chance of adding a view that when you select a file you can view the contents of that file?

AlonMiz commented 3 years ago

+1

ashtonian commented 3 years ago

224

yilmazdurmaz commented 2 years ago

That is definitely what I am looking for.

use case: Imagine you somehow accidentally deleted the source files you used to create that container. use case: Imagine you had a small detail when creating this image. You have created many images but this specific one works best but you forgot what setting you used in it, and your source files no longer has that small detail in them.

patsevanton commented 2 years ago

Any news?

jkopczyn commented 2 years ago

I, too, would appreciate this feature. I did something hacky with docker commit recently, and being able to check how exactly the files changed in dive would be very helpful towards the goal of replicating those changes in the build.

waldner commented 2 years ago

+1, FWIW

shivamdunzo310 commented 2 years ago

+1 to this feature

kamuridesu commented 2 years ago

+1

ChristopherLenz commented 1 year ago

+1

yurenchen000 commented 1 year ago

only for docker (I don't use podman)

Get image layer files

🔵 the way

docker save -o save.tar IMAGE:TAG

can got the image tar file, with layers inside. user can extact file from it.

seems dive also do it in that way (with docker client lib) //also that's the reason that dive load slowly //current version dive seems only analyze file list in tar https://github.com/wagoodman/dive/blob/v0.10.0/dive/image/docker/engine_resolver.go#L96

🔵 shell helper

layer_list(){
    local id="$1"
    local file="$2"
    tar -Oxf save.tar $id/layer.tar  | tar tvf - "$file"
}
layer_extract(){
    local id="$1"
    local file="$2"
    tar -Oxf save.tar $id/layer.tar  | tar xvf - "$file"
}

//after docker save got save.tar

usage demo

layer_list layer_id_seen_in_dive [file_path]

👉️ 💤 $ layer_list 20f751de6380bb8ded7794f97bc970eb2c45f53752236c6419fd6ecfa54e6310 etc/
drwxr-xr-x 0/0               0 2023-03-08 10:06 etc/
-rw------- 0/0               0 2023-03-08 10:05 etc/.pwd.lock
-rw-r--r-- 0/0            3028 2023-03-08 10:06 etc/adduser.conf
drwxr-xr-x 0/0               0 2023-03-08 10:06 etc/alternatives/
-rw-r--r-- 0/0             100 2018-04-16 10:25 etc/alternatives/README
lrwxrwxrwx 0/0               0 2023-03-08 10:06 etc/alternatives/awk -> /usr/bin/mawk
lrwxrwxrwx 0/0               0 2023-03-08 10:06 etc/alternatives/nawk -> /usr/bin/mawk
...

layer_extract layer_id_seen_in_dive [file_path]

👉️ 💤 $ layer_extract 20f751de6380bb8ded7794f97bc970eb2c45f53752236c6419fd6ecfa54e6310 etc/os-release
etc/os-release
$ ll etc/os-release 
lrwxrwxrwx 1 root root 21 Sep  6  2021 etc/os-release -> ../usr/lib/os-release


The effective (directly) way?

I don't known how to find the layer files in /var/lib/docker dir directly (it's storage driver related).

on docker 23 with overlay2 storage driver, I use this script to find the files very roughly

🔵 shell helper

find_layer_by_hash(){
    local hash="$1"
    local file="$2"
    echo "   hash: $hash"
    local layerdb=`head /var/lib/docker/image/overlay2/layerdb/sha256/*/diff | grep "$hash" -B1 -m1 | grep -o '/.*/' -`
    echo "layerdb: $layerdb"
    local overlay=`cat $layerdb/cache-id`
    echo "overlay: $overlay"
    echo "=== /var/lib/docker/overlay2/$overlay/diff"
    ls -hl /var/lib/docker/overlay2/$overlay/diff/"$file"
}

USAGE: find_layer_by_hash layer_hash_seen_in_dive [file_path]

👉️ 💤 $ find_layer_by_hash b7e0fa7bfe7f9796f1268cca2e65a8bfb1e010277652cee9a9c9d077a83db3c4 etc/os-release 
   hash: b7e0fa7bfe7f9796f1268cca2e65a8bfb1e010277652cee9a9c9d077a83db3c4
layerdb: /var/lib/docker/image/overlay2/layerdb/sha256/b7e0fa7bfe7f9796f1268cca2e65a8bfb1e010277652cee9a9c9d077a83db3c4/
overlay: 40c57ca91bfb85eb7226d8397c7832d905ee54bf580e1a5e1d3dde35e46f0e56
=== /var/lib/docker/overlay2/40c57ca91bfb85eb7226d8397c7832d905ee54bf580e1a5e1d3dde35e46f0e56/diff
lrwxrwxrwx 1 root root 21 Sep  6  2021 /var/lib/docker/overlay2/40c57ca91bfb85eb7226d8397c7832d905ee54bf580e1a5e1d3dde35e46f0e56/diff/etc/os-release -> ../usr/lib/os-release


the layer hash:

docker image inspect IMAGE:TAG | jq '.[].RootFS.Layers'
Kartik1397 commented 1 year ago

Thanks @yurenchen000 for figuring out the solution. I built a small tool around this which makes extracting layer a bit more easier. https://github.com/Kartik1397/docker-image-layer-explorer

valeriigamaley commented 11 months ago

genius hint from @yurenchen000. very nice & thx for that approach

TheRealGramdalf commented 8 months ago

Using docker create and docker cp is much simpler: https://unix.stackexchange.com/questions/331645/extract-file-from-docker-image