11

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

LeoMan
  • 139
  • 1
  • 1
  • 6

5 Answers5

17

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
张馆长
  • 1,321
  • 10
  • 11
  • so if there a folders in overlay2 which are not in the output of your command, they are not in use anymore? – rené Oct 17 '22 at 08:29
8

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.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • thanks. very helpful. i was able to show all images with `docker inspect -f $'{{.Id}}\t{{.RepoTags}}\t{{.GraphDriver.Data}}' $(docker image ls -q)` – Jayen Aug 22 '22 at 10:58
  • With a bit more processing for convenience, as JSON, but needs jq: `docker image ls -q | xargs docker inspect | jq '[.[]|{tags:.RepoTags,image:.Id,layers:([.GraphDriver.Data[]|split(":")]|flatten)}]'` – Hannes Apr 28 '23 at 20:39
1

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/

Vinod
  • 515
  • 4
  • 11
0

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"
venkat
  • 21
  • 1
0

Below command provides a good view of the containers and their volume, image and space usage.

docker system df -v
satishkumar432
  • 337
  • 1
  • 6
  • 19