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.