简体   繁体   中英

How to find which folder under overlay2 directory belong to which container?

If I go to /var/lib/docker/overlay2 and do "ls", then I get something like the following:

0349eb19b8ac737f2a93e33f665ab26d95de636bf48de79e6e7d7cdd76c0cd78 0c32910d4c394834e239f0c8f0776b36d87f7567b3120dff92ccd8d0f98577b0...

Now one or more of these folders belong to a container. I want to know which folder is used by which container?

Thanks

you can use docker inspect command to get all info regarding the container which will provide the info including the volume.

docker inspect <container name/id>

you can also name these volume with the help of named volume.

Ref: https://docs.docker.com/storage/volumes/

Now each of these folders belong to a container

No, they don't. A container may use one or more of these folders. But the same folder may be used in multiple containers. This is how layer reuse works in docker, it gets downloaded or built once, and then reused as a layer in the overlay filesystem.

Therefore, there's no mapping from the overlay2 directories to "a" container. But you can look at every container and image to see what directories it is using for the various layers:

$ docker container inspect --format '{{json .GraphDriver.Data }}' ${container_id} | jq .
{
  "LowerDir": "/var/lib/docker/overlay2/772a19168d50648e1bfb156604281a6acedb9025e588fe35da97deb18b95150a-init/diff:/var/lib/docker/overlay2/ixbbgqs1303yho2a3dcwwlw2u/diff:/var/lib/docker/overlay2/vdywc8lgvc3uzoq8z2m2j49io/diff:/var/lib/docker/overlay2/l4ykndb21ul7ttmsdn05ps3q5/diff:/var/lib/docker/overlay2/66fce03abb53f3576b039c5177e687f304d1a7ddb78d57392b8825ab34350299/diff",
  "MergedDir": "/var/lib/docker/overlay2/772a19168d50648e1bfb156604281a6acedb9025e588fe35da97deb18b95150a/merged",
  "UpperDir": "/var/lib/docker/overlay2/772a19168d50648e1bfb156604281a6acedb9025e588fe35da97deb18b95150a/diff",
  "WorkDir": "/var/lib/docker/overlay2/772a19168d50648e1bfb156604281a6acedb9025e588fe35da97deb18b95150a/work"
}

$ docker image inspect --format '{{json .GraphDriver.Data}}' ${image_name} | jq .
{
  "LowerDir": "/var/lib/docker/overlay2/vdywc8lgvc3uzoq8z2m2j49io/diff:/var/lib/docker/overlay2/l4ykndb21ul7ttmsdn05ps3q5/diff:/var/lib/docker/overlay2/66fce03abb53f3576b039c5177e687f304d1a7ddb78d57392b8825ab34350299/diff",
  "MergedDir": "/var/lib/docker/overlay2/ixbbgqs1303yho2a3dcwwlw2u/merged",
  "UpperDir": "/var/lib/docker/overlay2/ixbbgqs1303yho2a3dcwwlw2u/diff",
  "WorkDir": "/var/lib/docker/overlay2/ixbbgqs1303yho2a3dcwwlw2u/work"
}

At the filesystem level, you can also look at /var/lib/docker/containers/${container_id}/config.v2.json .

Note that all of this is docker internals, and there is no guarantee that it won't change in the next version. You should not be directly interacting with these files and directories, because docker does not know if you have made a breaking change and will not fix a broken image by downloading a layer again. As others have discovered, when you break it , you get to keep both pieces as a souvenir for what not to do.

could use this command

for container in $(docker ps --all --quiet --format '{{ .Names }}'); do
    echo "$(docker inspect $container --format '{{.GraphDriver.Data.MergedDir }}' | grep -Po '^.+?(?=/merged)'  ) = $container"
done

I use the below method to identify the container that uses such folders,

- docker ps | awk '{print $1}' (save it in a file - continerid)


 #./bin/bash input="./containerid" while IFS= read -r line do echo "$line" docker inspect $line | grep -i "5b....." done < "$input"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM