Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@travishein
Created July 13, 2017 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travishein/dfb443f446d70cbb9f3fea1c6cd915de to your computer and use it in GitHub Desktop.
Save travishein/dfb443f446d70cbb9f3fea1c6cd915de to your computer and use it in GitHub Desktop.
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