How do I identify which container owns which overlay directory?
My /var/lib/docker/overlay
directory contains data for my containers but the directory names are all hashed. How can I determine which container owns which overlay?
Solution 1:
You can use jq
like so:
docker inspect $(docker ps -qa) | jq -r 'map([.Name, .GraphDriver.Data.MergedDir]) | .[] | "\(.[0])\t\(.[1])"'
Which gives:
/traefik_traefik_1 /var/lib/docker/overlay/58df937e805ec0496bd09686394db421c129e975f67180e737d5af51751af49c/merged
/gitlab-runner /var/lib/docker/overlay/4e5b06f4ee09c60e2dad3a0ce87352cead086eb8229775f6839a477b46dce565/merged
/rancher-agent /var/lib/docker/overlay/6026bb65dd9a83c2088a05cff005a1dd824494222445bab46b7329dc331465aa/merged
Explanation:
docker inspect $(docker ps -qa)
Display full docker details.
jq -r
Parse json and output regular strings:
map([.Name, .GraphDriver.Data.MergedDir])
For each element in the original array, find the Name
and the overlay MergedDir
.
"\(.[0])\t\(.[1])"
Output the first two elements of the array.
Solution 2:
Thanks to @Matthew for his solution. It made me think of a Docker-only solution (without jq):
docker inspect -f $'{{.Name}}\t{{.GraphDriver.Data.MergedDir}}' $(docker ps -aq)
You can also ask for a specific container using
docker inspect -f $'{{.Name}}\t{{.GraphDriver.Data.MergedDir}}' <container_name>
Explanations
I use the -f
option of Docker inspect that allows me to use a Go template to format the output of docker inspect
.
I use $''
in Bash to allow special chars like \t
in my format.
Solution 3:
docker ps -qa | xargs -I{} bash -c "docker inspect {} | grep <hash> && echo {}"
which would give you, for example,
"LowerDir": "...<hash>...",
"MergedDir": "...<hash>...",
"UpperDir": "...<hash>...",
"WorkDir": "...<hash>..."
24f8ab7ae838
Now you can tell the container 24f8ab7ae838
owns the overlay <hash>
.