How to identify which Docker container an overlay is for?
Docker stores data under directories like /var/lib/docker/overlay2/xyz123
. These sometimes grow very large, and Docker does not provide good instructions for how to easily manage the space used by these. I assume IDs like xyz123
somehow correspond to containers.
Occasionally, I notice that one of these is very large, eg. 10 GB. If all I know is the path of the overlay dir, how can I figure out which container this was for, so I can decide if it's something that I can clean up?
Note: When this point is brought up, it often conjures long arguments about whether it's bad to mess with these overlay dirs manually. I'm not asking about deleting them manually in this question, so let's not get into that here. I am only asking how to identify which container/image is responsible for a given overlay directory.
1 answer
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
matthewsnyder |
Thread: Works for me Thank you! It looks like in the output of |
Nov 13, 2024 at 19:14 |
You can find that when you run docker inspect $CONTAINER
. You can automate this with a simple loop like this:
for CONTAINER in $(docker ps -qa);
do docker inspect $CONTAINER |grep -q $DIRNAME && docker ps -a |grep $CONTAINER;
done
where $DIRNAME
is the overlay directory name.
This will loop over all containers, grep the output of docker inspect
on it and if it finds the directory name it will run grep
with the container ID on the output of docker ps -a
, showing the container where it is mounted.
If you don't get any output the container where it belonged to doesn't exist anymore.
Example:
root@host:/var/lib/docker/overlay2# ls -1
e9341f72f22128ceeed7373a36bce28d02751bd0c51e60e1deaa8f77903f2f35
root@host:/var/lib/docker/overlay2# for CONTAINER in $(docker ps -qa); do docker inspect $CONTAINER |grep -q e9341f72f22128ceeed7373a36bce28d02751bd0c51e60e1deaa8f77903f2f35 && docker ps -a |grep $CONTAINER; done
458f26e907ff wordpress "docker-entrypoint.s…" 11 months ago Up 12 days 0.0.0.0:8081->80/tcp, [::]:8081->80/tcp wordpress-wordpress-1
1 comment thread