2023-11-17 16:53:56 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-11-17 18:31:35 +01:00
|
|
|
status=0
|
|
|
|
|
|
|
|
# The first argument is a space-separated list of whitelisted volume IDs
|
|
|
|
whitelist=$1
|
|
|
|
whitelisted_volumes=($whitelist) # Split into an array
|
|
|
|
|
2023-11-17 16:53:56 +01:00
|
|
|
anonymous_volumes=$(docker volume ls --format "{{.Name}}" | grep -E '^[a-f0-9]{64}$')
|
|
|
|
|
|
|
|
if [ -z "$anonymous_volumes" ]; then
|
|
|
|
echo "No anonymous volumes found."
|
2023-12-24 19:40:39 +01:00
|
|
|
exit
|
2023-11-17 16:53:56 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Anonymous volumes found:"
|
|
|
|
|
|
|
|
for volume in $anonymous_volumes; do
|
2023-11-17 18:31:35 +01:00
|
|
|
# Check if the volume is in the whitelist
|
|
|
|
if printf '%s\n' "${whitelisted_volumes[@]}" | grep -q "^$volume$"; then
|
|
|
|
echo "Volume $volume is whitelisted and will be skipped."
|
|
|
|
continue
|
|
|
|
fi
|
2023-11-17 16:53:56 +01:00
|
|
|
|
2023-12-28 23:59:05 +01:00
|
|
|
((status++))
|
2023-12-29 19:55:26 +01:00
|
|
|
|
2023-11-17 18:31:35 +01:00
|
|
|
container_ids=$(docker ps -aq --filter volume=$volume)
|
2023-11-17 16:53:56 +01:00
|
|
|
if [ -z "$container_ids" ]; then
|
|
|
|
echo "Volume $volume is not used by any running containers."
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
for container_id in $container_ids; do
|
|
|
|
container_name=$(docker inspect --format '{{ .Name }}' $container_id | sed 's#^/##')
|
|
|
|
mount_path=$(docker inspect --format "{{ range .Mounts }}{{ if eq .Name \"$volume\" }}{{ .Destination }}{{ end }}{{ end }}" $container_id)
|
|
|
|
|
|
|
|
if [ -n "$mount_path" ]; then
|
|
|
|
echo "Volume $volume is used by container $container_name at mount path $mount_path"
|
|
|
|
else
|
|
|
|
echo "Volume $volume is used by container $container_name, but mount path could not be determined."
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
2023-11-17 18:31:35 +01:00
|
|
|
exit $status
|