Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
shows which docker containers currently depend on a given docker volume
#!/bin/bash
# shows which docker containers currently depend on a given docker volume
volume_name="${1}";
if [ -z "${volume_name}" ]; then
echo "ERROR: require volume name as input parameter.";
exit 1;
fi;
mountpoint="`docker volume inspect ${volume_name} 2>/dev/null | jq -r '.[].Mountpoint'`";
if [ -z "${mountpoint}" ]; then
echo "ERROR: no volume found for name: ${volume_name}";
exit 1;
fi;
docker ps -a --format "{{.ID}}\t{{.Names}}" | while read container_id container_name; do
length="`docker inspect ${container_id} | jq '.[].Mounts | length'`";
for (( c=0; c<${length}; c++ )); do
source="`docker inspect ${container_id} | jq -r ".[].Mounts[${c}].Source"`";
if [ "${source}" == "${mountpoint}" ]; then
echo "${container_id} ${container_name}";
fi;
done;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment