2017-04-25 10:08:07 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
|
|
echo "List of all volumes:"
|
2017-04-25 10:09:58 +02:00
|
|
|
echo
|
|
|
|
|
2017-04-25 10:12:39 +02:00
|
|
|
#Loop over all the data volumes
|
2017-04-25 10:08:07 +02:00
|
|
|
for docker_volume_id in $(docker volume ls -q)
|
|
|
|
do
|
2017-04-25 10:09:58 +02:00
|
|
|
echo "(Un)named volume: ${docker_volume_id}"
|
2017-04-25 10:08:07 +02:00
|
|
|
|
2017-04-25 10:12:39 +02:00
|
|
|
#Obtain the size of the data volume by starting a docker container
|
|
|
|
#that uses this data volume and determines the size of this data volume
|
2017-04-25 10:08:07 +02:00
|
|
|
docker_volume_size=$(docker run --rm -t -v ${docker_volume_id}:/volume_data ubuntu bash -c "du -hs /volume_data | cut -f1" )
|
|
|
|
|
2017-04-25 10:09:58 +02:00
|
|
|
echo " Size: ${docker_volume_size}"
|
2017-04-25 10:08:07 +02:00
|
|
|
|
2017-04-25 10:12:39 +02:00
|
|
|
#Determine the number of stopped and running containers that
|
|
|
|
#have a connection to this data volume
|
2017-04-25 10:08:07 +02:00
|
|
|
num_related_containers=$(docker ps -a --filter=volume=${docker_volume_id} -q | wc -l)
|
|
|
|
|
2017-04-25 10:12:39 +02:00
|
|
|
#If the number is non-zero, we show the information about the container and the image
|
|
|
|
#and otherwise we show the message that are no connected containers
|
2017-04-25 10:08:07 +02:00
|
|
|
if (( $num_related_containers > 0 ))
|
|
|
|
then
|
2017-04-25 10:09:58 +02:00
|
|
|
echo " Connected containers:"
|
2017-04-25 10:08:52 +02:00
|
|
|
docker ps -a --filter=volume=${docker_volume_id} --format "{{.Names}} [{{.Image}}] ({{.Status}})" | while read containerDetails
|
2017-04-25 10:08:07 +02:00
|
|
|
do
|
2017-04-25 10:09:58 +02:00
|
|
|
echo " ${containerDetails}"
|
2017-04-25 10:08:07 +02:00
|
|
|
done
|
|
|
|
else
|
2017-04-25 10:09:58 +02:00
|
|
|
echo " No connected containers"
|
2017-04-25 10:08:07 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo
|
|
|
|
done
|