mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2024-11-29 16:01:04 +01:00
53 lines
1.6 KiB
Bash
53 lines
1.6 KiB
Bash
#!/bin/bash
|
|
|
|
status=0
|
|
|
|
# The first argument is a space-separated list of whitelisted volume IDs
|
|
whitelist=$1
|
|
whitelisted_volumes=($whitelist) # Split into an array
|
|
|
|
anonymous_volumes=$(docker volume ls --format "{{.Name}}" | grep -E '^[a-f0-9]{64}$')
|
|
|
|
if [ -z "$anonymous_volumes" ]; then
|
|
echo "No anonymous volumes found."
|
|
exit
|
|
fi
|
|
|
|
echo "Anonymous volumes found:"
|
|
|
|
for volume in $anonymous_volumes; do
|
|
# 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
|
|
|
|
status=1
|
|
container_ids=$(docker ps -aq --filter volume=$volume)
|
|
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
|
|
|
|
# Additional warning for dangling volumes
|
|
dangling_volumes=$(docker volume ls -f dangling=true --format "{{.Name}}")
|
|
if [ -n "$dangling_volumes" ]; then
|
|
status=2
|
|
echo "The following dangling volumes were found:"
|
|
echo "$dangling_volumes"
|
|
fi
|
|
|
|
exit $status
|