From 3a001879fc61d2cc0ff7323b94701af0c50e6b00 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Fri, 21 Oct 2022 14:34:51 +0200 Subject: [PATCH] fix: [compose,compose-core] revert detection of used volume This is not iso-functional and can't detect if a subvolume was mounted. Signed-off-by: Valentin Lab --- bin/compose | 28 ++++++++++++++++++++++------ bin/compose-core | 14 ++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/bin/compose b/bin/compose index 765b754..9f15b41 100755 --- a/bin/compose +++ b/bin/compose @@ -308,14 +308,30 @@ list_compose_vars() { done < <(declare -p | grep "^declare -x COMPOSE_[A-Z_]\+=\"") } +get_running_compose_containers() { + ## XXXvlab: docker bug: there will be a final newline anyway + docker ps --filter label="compose.service" --format='{{.ID}}' +} + +get_volumes_for_container() { + local container="$1" + + docker inspect \ + --format '{{range $mount := .Mounts}}{{$mount.Source}}{{"\x00"}}{{$mount.Destination}}{{"\x00"}}{{end}}' \ + "$container" +} + + is_volume_used() { - local volume="$1" out - if ! out=$(docker ps --filter volume="$volume" --format {{.ID}}); then - err "Couldn't query containers by volumes." - return 0 - fi - [ -n "$out" ] + local volume="$1" + + while read container_id; do + while read-0 src dst; do + [ "$src" == "$volume" ] && return 0 + done < <(get_volumes_for_container "$container_id") + done < <(get_running_compose_containers) + return 1 } diff --git a/bin/compose-core b/bin/compose-core index 112f867..4ad4ba2 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -3325,12 +3325,14 @@ get_volumes_for_container() { export -f get_volumes_for_container is_volume_used() { - local volume="$1" out - if ! out=$(docker ps --filter volume="$volume" --format {{.ID}}); then - err "Couldn't query containers by volumes." - return 0 - fi - [ -n "$out" ] + local volume="$1" container_id src dst + + while read -r container_id; do + while read-0 src dst; do + [[ "$src/" == "$volume"/* ]] && return 0 + done < <(get_volumes_for_container "$container_id") + done < <(get_running_compose_containers) + return 1 } export -f is_volume_used