Extracting All Docker Volume Labels With No Name Association And Adding Them to a List

31 Views Asked by At

I'm trying to extract all the Docker Volume labels with no name association to a container and add them to a list. Then from that list, I want to compare with the other no-name docker volume labels that have a container attached to the volumes. The container names of the no-name docker volume labels are "frontend" and "backend." I want to go through each no-name docker volume label in the no-name association list with no containers attached and check against the no-name docker volumes that do have a container attached to them. If the current item in the list doesn't match with any of the no-name docker volumes with a container attached, I want to remove those docker volume labels in the list.

This was my script for it using Shell Scripting:

docker volume ls
docker volume ls -qf dangling=true
arr=()
arr+=$(docker volume ls -qf dangling=true)
for n in $(docker volume ls -qf dangling=true); do
if [[ $n != docker inspect --format "{{.Mounts}}" frontend || $n != docker inspect --format "{{.Mounts}}" backend ]]
then
    docker volume rm $(docker volume ls -qf dangling=true)
    done
fi

But when I executed the Shell script on Git Bash, I just get the execution of the 2nd command and it doesn't pass through the for loop. It still shows the list of all the dangling docker volumes. But not in an array-type format that is horizontal.

0

There are 0 best solutions below