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