Created
July 13, 2017 13:44
-
-
Save travishein/dfb443f446d70cbb9f3fea1c6cd915de to your computer and use it in GitHub Desktop.
shows which docker containers currently depend on a given docker volume
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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